bt707
asked on
push file to hash
I'm adding some work to a script we have, having one issue here.
I have this now in my script:
-------------------------- ---------- ------
if(opendir(DIR, $my_out)) {
print qq{ Working through \@abc.com files in $my_out\n};
foreach my $file (readdir(DIR)) {
if ((-f qq{$my_out/$file}) && ($file =~m/\@abc.com/))
{print qq{ $file is in directory\n};
}
}
-------------------------- ---------- ------
This part will print out a list of files if they contain @abc.com which is what I need.
What I need to do is change the print of the files I have above to create a %array with
the file names it prints out currently.
I need to put this list of files in a hash because of what I need to do later in the script.
Not sure how I put this list of file in a hash, do I just use push or how can I do this?
Thanks,
I have this now in my script:
--------------------------
if(opendir(DIR, $my_out)) {
print qq{ Working through \@abc.com files in $my_out\n};
foreach my $file (readdir(DIR)) {
if ((-f qq{$my_out/$file}) && ($file =~m/\@abc.com/))
{print qq{ $file is in directory\n};
}
}
--------------------------
This part will print out a list of files if they contain @abc.com which is what I need.
What I need to do is change the print of the files I have above to create a %array with
the file names it prints out currently.
I need to put this list of files in a hash because of what I need to do later in the script.
Not sure how I put this list of file in a hash, do I just use push or how can I do this?
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adam314,
That worked great, I think I can get what I want figured out now.
Thank you very much!!
That worked great, I think I can get what I want figured out now.
Thank you very much!!
If all the values are the same, what is the purpose of using a hash rather than an array?
If it is to merge duplicates, there shouldn't be duplicates in a single DIR
If it is to merge duplicates, there shouldn't be duplicates in a single DIR
ASKER
ozo,
I guess a array in this case, I was taking parts of another script in making this one and using the same way it worked there, what I'm doing is a number of things with perl link and unlink from a couple of directories then if a address is a users short address, i.e user@abc.com, then using net:ldap to do a ldapsearch then pulling there full address from ldap using the short address then finishing from there, the files in the directories are named based on the users mail address and I need to link to the full address.
It is working this way now but I think changing it to a array would make more since.
Thanks for you comments.
I guess a array in this case, I was taking parts of another script in making this one and using the same way it worked there, what I'm doing is a number of things with perl link and unlink from a couple of directories then if a address is a users short address, i.e user@abc.com, then using net:ldap to do a ldapsearch then pulling there full address from ldap using the short address then finishing from there, the files in the directories are named based on the users mail address and I need to link to the full address.
It is working this way now but I think changing it to a array would make more since.
Thanks for you comments.
For an array...
my @array;
if(opendir(DIR, $my_out)) {
print qq{ Working through \@abc.com files in $my_out\n};
foreach my $file (readdir(DIR)) {
if ((-f qq{$my_out/$file}) && ($file =~m/\@abc.com/)) {
print qq{ $file is in directory\n};
push @array, $file;
}
}
}
if you want a hash, are the file names the keys or the values?
if they are keys, what are the values? if they are values, what are the keys?
what do you do later in the script?