Your unusual formatting makes it a bit hard to see what you're trying to do. But to merge two hashes is relatively simple:
$ cat hashmerge.pl #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %H1 = ( apple=>1, banana=>25, lemon=>14 ); my %H2 = ( apple=>7, banana=>14, geranium=>9001 ); # To merge, just copy the first hash in, then insert selected # values from second hash my %merged = %H1; for my $k (keys %H2) { if (exists $merged{$k}) { # We already have one of these. if ($H2{$k} < $merged{$k}) { # Second hash has a "better" value $merged{$k} = $H2{$k}; } } else { # we don't have one of these yet, so add it $merged{$k} = $H2{$k}; } } print Dumper(\%merged); $ perl hashmerge.pl $VAR1 = { 'banana' => 14, 'geranium' => 9001, 'apple' => 1, 'lemon' => 14 };
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re: Merging two lists
by roboticus
in thread Merging two lists
by bvulner
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |