bvulner has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone.. I'm new to perl.. I've been searching around but I didn't come up with a solution for my problem, maybe you can give me a hint. I have two hash and I have to merge them. But also, if they appear to have a common element, I have to choose the one with the lowest score to appear in the new hash. Let's say I have this two elements:

car coord bike 0.6 car random cat 0.1 car mero door 0.6
and
truck random dog 0.3 truck mero door 0.5
What I'd like to obtain is:
car_truck coord bike 0.6 car_truck random cat 0.1 car_truck mero door 0.5 car_truck random dog 0.3
I'm stuck at this point with no idea on how to progress:
#!/usr/bin/perl -w #use 5.010; use strict; use warnings; use Getopt::Std; use Data::Dumper; my $usage; { $usage = <<"_USAGE_"; _USAGE_ } my %opts = (); getopts('h',\%opts); if ($opts{h}) { print $usage; exit; } #Crea la matrice di NON_CONCETTI my $prefix = shift; my $input = shift; #matrice_ridotta my $input_coppie =shift; #coppiediparole my $file = $prefix . ".txt"; if (-e $file) { print STDERR "$file already exists, deleting previous version\n"; `rm -f $file`; } open INPUTCOPPIE, $input_coppie; my %map; while (<INPUTCOPPIE>) { my ($first, $second) = split; $map{$first}=$second; } close INPUTCOPPIE; #print Dumper \%map; open INPUT, $input; my %HOH; while (<INPUT>) { my ($nome, $c1, $c2, $c3) = split; my $prop = $c1."_".$c2; $HOH{$nome}{$prop} = $c3 || 0; } close INPUT; my ($first, $second,$int); my %hashintermedio; while (my($ele,$coord)=each(%map)){ if (exists $HOH{$ele}){ $first=$HOH{$ele}; if(exists $HOH{$coord}){ $second=$HOH{$coord}; while (my ($prop,$score)=each %$second){ } } } } while (my($ele,$coord) = each (%map)) { if (exists $HOH{$ele}){ $first = $HOH{$ele}; if (exists $HOH{$coord}){ $second = $HOH{$coord}; while (my($prop,$score) = each %$second ) { if (not exists $first->{$prop}){ $hashintermedio{$int}{$prop}=$score } else } } } else { next; } }
I'm getting lost and I don't know where to find a hint. Any suggestions will be really appreciated.

Replies are listed 'Best First'.
Re: Merging two lists
by roboticus (Chancellor) on Jun 19, 2012 at 16:54 UTC

    bvulner:

    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.

Re: Merging two lists
by toolic (Bishop) on Jun 19, 2012 at 17:19 UTC
    use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my @lines1 = split /\n/, <<EOF; car coord bike 0.6 car random cat 0.1 car mero door 0.6 EOF my @lines2 = split /\n/, <<EOF; truck random dog 0.3 truck mero door 0.5 EOF my %data; for (@lines1, @lines2) { my ($k1, $k2, $v) = (split)[1 .. 3]; my $k = "$k1 $k2"; if (exists $data{$k}) { $data{$k} = $v if $data{$k} > $v; } else { $data{$k} = $v; } } print Dumper(\%data); __END__ $VAR1 = { 'coord bike' => '0.6', 'mero door' => '0.5', 'random cat' => '0.1', 'random dog' => '0.3' };