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:
andcar coord bike 0.6 car random cat 0.1 car mero door 0.6
What I'd like to obtain is:truck random dog 0.3 truck mero door 0.5
I'm stuck at this point with no idea on how to progress: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 getting lost and I don't know where to find a hint. Any suggestions will be really appreciated.#!/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; } }
In reply to Merging two lists by bvulner
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |