[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

hashes of arrays

Posted on 2009-04-16
3
Medium Priority
?
245 Views
Last Modified: 2012-05-06
I have a file like this:

line1:123456:yes:update
line2:456789:no:update
line3:741852:yes:nupdate

and was wondering how if it's possible for me to create a hash, whose keys are giong to be the first element in each line of this file and the rest will just be values for that key


also after i have this created i need to grep each array in in the hash to see if the second element matches any element is found in another array that looks like this:

array = [789633, 963852, 159753]

can somebody help me? thanks! :D
0
Comment
Question by:cucugirl
  • 2
3 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 24161413
my %hash;
while( <DATA> ){
  chomp;
  my @array = split/:/;
  $hash{shift @array} = [@array];
}

my $array=[789633, 963852, 159753];
my %array;
@array{@$array}=();
for( values %hash ){
print "@$_\n" if exists $array{$_->[1]}
}
__DATA__
line1:123456:yes:update
line2:456789:no:update
line3:741852:yes:nupdate
0
 
LVL 5

Accepted Solution

by:
vikaskhoria earned 2000 total points
ID: 24167190
Check the this script if this helps.
If you name the script as abc.pl and text file as zyx.txt
then run as:
$ abc.pl xyz.txt

Do let me know, if you need any clarifications.

#!/usr/bin/perl
 
my %fileData;
@arr = (789633, 963852, 159753, 741852);
while(<>)
{
        @line = split(/:/, $_);
        $fileData{shift @line} = [@line];
}
 
for $value (@arr)
{
 
        for $line ( keys %fileData )
        {
                print $value." ".$fileData{$line}[1]."\n";
                if ( $value == $fileData{$line}[1])
                {
                        print "\Found! $value\n";
                }
        }
}
~   

Open in new window

0
 
LVL 85

Expert Comment

by:ozo
ID: 24174328
Comparing with every element of an array can be a lot slower than looking up a hash.

And something seems absurd about searching through a hash looking for a value.
Is there a reason why you want to index by $line[0] instead of by $line[1]?
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…
Six Sigma Control Plans
Suggested Courses

873 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