Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Retrieving Key and Value Hashes

by AnomalousMonk (Archbishop)
on Jul 05, 2015 at 01:18 UTC ( [id://1133220]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Retrieving Key and Value Hashes
in thread Retrieving Key and Value Hashes

is there a way to integrate what you [AnonyMonk] are scripting into my script?

As the AnonyMonk says, it's a here document (see also the discussion of here-docs in Quote and Quote-like Operators in perlop), another way of defining a Perl string. To use the AnonyCode in your program, open the files by the name of an external file (not by reference to an internal string) just as you were doing in your OPed script.

i modified it ... and it appeared not work.

Problem descriptions like "it doesn't work" are almost useless. Can you please be more specific? How does it not work? Please see I know what I mean. Why don't you?.


Give a man a fish:  <%-(-(-(-<

Replies are listed 'Best First'.
Re^4: Retrieving Key and Value Hashes
by gghelpneeded (Novice) on Jul 05, 2015 at 03:02 UTC

    i tried to simply insert my files instead. on the command line it didnt print anything. no syntax errors. been working through possible edits.

    open(FILE1, $ARGV[0]) or die "Cannot open the file: $!"; my %hash1 = split ' ', do{ local $/; <FILE1> }; close FILE1; open(FILE2, $ARGV[1]) or die "Cannot open the file: $!"; while(<FILE2>) { my ($key) = split; exists $hash1{$key} and print "$key $hash1{$key}\n"; } close FILE2;
      Unless I missed something, your revised script (based on Anonymous Monk's suggestion) should work with the data you've shown. If it does not print anything, then, probably, the data is not exactly what you think.

      I would suggest that you add some debugging statements to the first part of your script, for example as follows:

      use Data::Dumper; open(FILE1, $ARGV[0]) or die "Cannot open the file: $!"; my %hash1 = split ' ', do{ local $/; <FILE1> }; close FILE1; print Dumper \%hash1;
      and provide the printed output (or part thereof if it is large).

      Update: Fixed a typo in the code above. Thanks to AnomalousMonk for having picked it and informed me.

      Here's one approach:

      Data file 1 file_1.dat:

      Data file 2 file_2.dat:

      Script check_hash_vs_keys_1.pl:

      Output:

      c:\@Work\Perl\monks\gghelpneeded>perl check_hash_vs_keys_1.pl file_1.d +at file_2.dat 'name1' -> 'ABCD' 'name4' -> 'AAAAA'


      Give a man a fish:  <%-(-(-(-<

      What is REPMOD ?

        it was the name of the original file, changed it to make it generic. my mistake.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found