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.


In reply to Merging two lists by bvulner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.