Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: hash of hashes

by shmem (Chancellor)
on Nov 23, 2006 at 11:18 UTC ( [id://585699]=note: print w/replies, xml ) Need Help??


in reply to hash of hashes

Perhaps this works:
$file = 'test_predictions_1'; open (FH, "<EXAMPLE"); while (<FH>) { chop; # remove trailing newline $id = $_; if (exists $data{$file}{$id}) {print "match\n";} }

<update>

Having a second look at your code:

if (exists $data{$file}{$id}) {print "match\n";}

Does the key $file to the hash $data even exist in the hash?

You could use Data::Dumper to dump your HoH and check if it's what you expect.

</update>

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: hash of hashes
by Anonymous Monk on Nov 23, 2006 at 12:11 UTC
    i have done he following:
    foreach my $file (@find) { print "file: $file\n"; open (LOOKUP, "<upload_profile/$file") or die $!; while (<LOOKUP>) { ($name, $id) = (split m{\t})[3, 4]; #print "id: $id\n"; #$data{$file} = {$id => $refName}; $data{$file}{$id} = $name; } } close (LOOKUP); for $file ( keys %data ) { print "$file: "; for $role ( keys %{ $data{$file} } ) { print "$id=$data{$file}{$id} "; } print "\n"; }
    but it seems to return only the last line of each file

      Data::Dumper is your friend. I suspect you will find that if you Dump out %data between your first loop and your second, that you will find that you're storing more than the last line of each file. However strict is even more your friend, and could have saved you the time in posting on here.

      If your second loop is a direct cut-n-paste then your error should be easily fixed. The following line:

      print "$id=$data{$file}{$id} ";

      Should probably be:

      print "$role=$data{$file}{$role} ";

      If you were using strict, Perl would have told you about this.

      The following code works for me.

      use strict; my %data; foreach my $file (@ARGV) { print "file: $file\n"; open (LOOKUP, "$file") or die $!; while (<LOOKUP>) { chomp; my ($name, $id) = (split m{\t})[3, 4]; $data{$file}{$id} = $name; } close (LOOKUP); } for my $file ( keys %data ) { print "$file: "; for my $role ( keys %{ $data{$file} } ) { print "$role=$data{$file}{$role} "; } print "\n"; }

      Be aware that if any id is repeated in the file for a different name, then you will only get the last unique pair. For example if your file contains:

      xxx yyyy Fred 4 zzzz wwww James 4

      Then you will only get the James => 4 pairing and Fred will be lost. If this is a problem, then you'll need to use an array reference:

      use strict; my %data; foreach my $file (@ARGV) { print "file: $file\n"; open (LOOKUP, "$file") or die $!; while (<LOOKUP>) { chomp; my ($name, $id) = (split m{\t})[3, 4]; push @{$data{$file}{$id}}, $name; } close (LOOKUP); } for my $file ( keys %data ) { print "$file: "; for my $role ( keys %{ $data{$file} } ) { for my $name ( @{$data{$file}{$role}} ) { print "$role=$name "; } } print "\n"; }

      Hope this helps.

Re^2: hash of hashes
by Anonymous Monk on Nov 23, 2006 at 11:21 UTC
    i have tried chomp and chop before posting with no success

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://585699]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-23 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found