in reply to Re^3: Need advice on checking two hashes values and keys
in thread Need advice on checking two hashes values and keys

cool. the square brackets was the correct method to do a ref to array...missed it although I read about it last night.thanks.

several questions

PROBLEM: the array seem to list of all of the numbers found in both files. I want to only print out the numbers that are fund on BOTH in one file then the numeri not found in the other into another file

my IF check doesn't seem to work?

push @{$hash{$key}},$value if $hash{$key}; #non so come funziona "push"

how is this piece of code work? I don't understand the "[1]", I think +it's a matrix , is it placement spot??
$hash{$italian}[1] = $french;

Replies are listed 'Best First'.
Re^5: Need advice on checking two hashes values and keys
by aaron_baugher (Curate) on Jun 05, 2015 at 02:01 UTC

    Okay, if there could be numbers in one file but not the other (I forgot about that in your original data), you won't be able to use the push method, because numbers that weren't in the Spanish file will end up getting the French word in the Spanish spot. So you'll need to do it this way:

    $hash{$italian}[1] = $french;

    What that does is looks up the array pointed to by the reference in $hash{$italian}, and sets the second element of the array to the value in $french. The cool part is that $hash{$italian} doesn't even have to exist yet; it will be created automatically if it doesn't (that's called autovivification). So numbers that are only in the Spanish file will only have the first element of the array set, and numbers only in the French file will only have the second element set (the first element will be undef). So you'll end up with something like this (I made my own smaller input files to show what I mean):

    # Italian -> Spanish input file with 1 & 3 uno = uno tre = tres # Italian -> French input file with 2 & 3 due = deux tre = trois # the hash will be: $hash = ( uno => [ 'uno' ], due => [ undef, 'deux' ], tre => [ 'tres','trois' ], );

    See how the missing Spanish word for 2 is undefined, since we inserted the French word into the second element of the array? Now to print them out, you can check each array to see if both elements are present, and print to one file if they are, and the other file if they aren't:

    for my $key (keys %hash){ if( $hash{$key}[0] and $hash{$key}[1] ){ # both are present print $out "$key => ", join( ' , ', @{$hash{$key}}), "\n"; } else { # one is missing print $out1 "$key => ", join( ' , ', @{$hash{$key}}), "\n"; } }

    Now, in the else portion, you'll get a warning when it prints out an undefined value. You can ignore that, since you're expecting it, or turn off warnings in that section, or deal with it by putting in some "print if it exists" logic. That's up to you, and depends somewhat on what you want that second file to tell you about what's missing.

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

      I have corrected my piece of code with our help.

      After this is completed then I will add more numbers/complexity to make a list of list program. will add German and English in random order then check for keys and values.

      then moving to hash tables as per earlier suggestion

      I understand why I can not use the push function(due to french word in place of Spanish , if no Spanish word found)

      Still, I see a PROBLEM with my unless section. I am coming up with an empty matrix. trying to use only one hash...

      while(<$in1>){ chomp; my ($ita,$fran)=split /\s*=\s*/; #diventa in due colonne $hash{$ita}[1]=$fran; #salva i numeri francesi al loro posto [1] #se i numeri esiste in ambe matrice if ($hash{$ita}[0] and $hash{$ita}[1]){ print $out "$ita =>",join(',',@{$hash{$ita}}),"\n"; } else { print $out1 "$ita =>", join(',',@{$hash{$ita}}),"\n"; } } close $in1; close $out; close $in1;

        I'm not sure I understand your problem; you might need to show what the output looks like. But if this is your actual code, one problem is that your if/else statement which does the printing is inside your while() loop that adds the French values, so it's going to print the whole thing every time it processes a line from your Italian-French file. That if/else section should be after the while loop, in its own for loop, as I showed in my last post to which you replied.

        In other words, you'll have three independent loops:

        • A while loop to process the first file
        • A while loop to process the second file
        • A for loop to go through the hash and print the keys and values

        None of these should be within the others.

        Aaron B.
        Available for small or large Perl jobs and *nix system administration; see my home node.