in reply to Retrieving Key and Value Hashes

#!/usr/bin/perl # http://perlmonks.org/?node_id=1133207 use strict; $| = 1; my $file1 = <<END; name1 ABCD name2 BCD name3 ASCDA name4 AAAAA END my $file2 = <<END; name1 0.2 name5 0.3 name4 0.0002 name7 0.222 END open my $one, '<', \$file1 or die "$! opening file1"; my %hash1 = split ' ', do{ local $/; <$one> }; close $one; #use YAML; print Dump \%hash1; open my $two, '<', \$file2 or die "$! opengin file2"; while(<$two>) { my ($key) = split; exists $hash1{$key} and print "$key $hash1{$key}\n"; } close $two;

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

    what is the purpose behind setting the files equal to END?

      It's a here-document described in perlop.

Re^2: Retrieving Key and Value Hashes
by gghelpneeded (Novice) on Jul 05, 2015 at 00:17 UTC

    i have a secondary question. i modified it to work with a bigger file and it appeared not work. is there a way to integrate what you are scripting into my script?

      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:  <%-(-(-(-<

        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;

      Probably.