Solved

push file to hash

Posted on 2009-03-31
6
214 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This video discusses moving either the default database or any database to a new volume.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now