Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^5: Need advice on checking two hashes values and keys

by aaron_baugher (Curate)
on Jun 05, 2015 at 02:01 UTC ( [id://1129150]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^6: Need advice on checking two hashes values and keys
by Anonymous Monk on Jun 09, 2015 at 18:41 UTC

    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.

        I see the problem with doing the check within the while loop.

        I wanted to only use one hash but I see I need more than one.

        ok, let me give it a shot as per your suggestion

        totally a different question, I have modify my NEXT test program to include other numbers and I need your expert help with regexp...can you help me construct the regexp? I have a question on a word that maybe or not be in the string and be separated by a comma

        for example:

        dieci = diez, zehn quattro = quatro sei = seis, sette = siete,sechs
        while ($in){ #data manipulation to clean up ='s and ,'s chomp; $_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1129150]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found