jimmy78 has asked for the wisdom of the Perl Monks concerning the following question:
I am tryngi to create an array of 10 hashes and then trying to merge all 9 hashes in 1st hash. But second for loop doing it does not produce any output. Can somebody tell me what is wrong in the way I am access these hashes from the array. Jimmy update-> Now printed and added code for the subroutine for creating hashmy @a; for (my $i=0;$i<10;$i++) { my $filenum_str = sprintf "%03d", $i; my $filename = "/var/tmp/${filenum_str}"; my %b; create_hash_for_filename($filename,\%b); push @a, {%b}; } for my $key (keys %{$a[0]}) { for my $j (1..$#a) { if (exists $a[$j]{$key}) { #adding column 1 and 2 in 1st hash if found in other hashe +s and deleting from them $a[0]{$key}[0] +=$a[$j]{$key}[0]; $a[0]{$key}[1] +=$a[$j]{$key}[1]; # delete 1st and 2 nd column from other hashes delete $a[$j]{$key}; } } } print "now printing merged firat hash: for my $key (keys %{$a[0]}) { print "$key : $a[0]{$key}[0]: $a[0]{$key}[0] \n"; } sub create_hash_for_filename { my ($file , $hash) = @_; open (FILE, "< $file") or do { return(1,"$file couldnt be opened"); }; while(<FILE>) { my @match = split (' ', $_); $hash->{$match[0]} = [$match[1], $match[2]]; } close(FILE); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: array of hashes question
by ikegami (Patriarch) on Feb 18, 2009 at 16:31 UTC | |
by jimmy78 (Initiate) on Feb 18, 2009 at 16:38 UTC | |
by ikegami (Patriarch) on Feb 18, 2009 at 17:26 UTC | |
|
Re: array of hashes question
by targetsmart (Curate) on Feb 18, 2009 at 16:27 UTC | |
by jimmy78 (Initiate) on Feb 18, 2009 at 16:37 UTC | |
by jimmy78 (Initiate) on Feb 18, 2009 at 16:54 UTC | |
by jimmy78 (Initiate) on Feb 18, 2009 at 17:06 UTC | |
by jimmy78 (Initiate) on Feb 18, 2009 at 17:11 UTC | |
by Anonymous Monk on Feb 18, 2009 at 19:30 UTC | |
|
Re: array of hashes question
by Bloodnok (Vicar) on Feb 18, 2009 at 16:16 UTC | |
by jimmy78 (Initiate) on Feb 18, 2009 at 16:24 UTC |