• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • 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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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