Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^5: Need advice on checking two hashes values and keys by aaron_baugher
in thread Need advice on checking two hashes values and keys by perlynewby

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-28 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found