• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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,
0
bt707
Asked:
bt707
  • 2
  • 2
  • 2
1 Solution
 
ozoCommented:
do you want an array or a hash?
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?
0
 
Adam314Commented:
Assuming with the hash, you want the file names to be the keys, and the values to be 1, you'd use this:
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};
            $array{$file}=1;
        }
    }
}

Open in new window

0
 
bt707Author Commented:
Adam314,

That worked great, I think I can get what I want figured out now.

Thank you very much!!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ozoCommented:
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
0
 
bt707Author Commented:
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.
0
 
Adam314Commented:
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;
        }
    }
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now