Solved

push file to hash

Posted on 2009-03-31
6
219 Views
Last Modified: 2012-05-06
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
Comment
Question by:bt707
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24034191
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
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 24034194
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
 

Author Closing Comment

by:bt707
ID: 31565085
Adam314,

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

Thank you very much!!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 24034470
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
 

Author Comment

by:bt707
ID: 24034615
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
 
LVL 39

Expert Comment

by:Adam314
ID: 24039931
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question