gghelpneeded has asked for the wisdom of the Perl Monks concerning the following question:

below is a question i need help addressing. the script works in the way i want it to. i just need help finishing it. my script takes one file and creates a hash from it and then takes the second file and creates a key from it. the last part is suppose to take the keys that are the same and print out the corresponding value along with the key from the first file. I can get the key but not the value

heres and example of the data.

file 1

name1 ABCD

name2 BCD

name3 ASCDA

name4 AAAAA

file 2

name1 0.2

name5 0.3

name4 0.0002

name7 0.222

my %s; open(FILE1, $ARGV[0]) or die "Cannot open the file: $!"; my $hash_key; my $hash_value; while (my $line = <FILE1>) { if ($line =~ /^>(\S+)/) { $hash_key = $1; } elsif ($line =~ /\S/) { chomp $line; $hash_value = $line; $s{$hash_key} = $hash_value; } } foreach my $hash_key1 (keys %s) { print "$hash_key1\t$s{$hash_key1}\n"; } my %n; open(FILE2, $ARGV[1]) or die "Cannot open the file: $!"; my $hash_key2; my $hash_value2; while (my $line2 = <FILE2>) { if ($line2 =~ /(\S*)/) { $hash_key2 = $1; $hash_value2 = $line2; $n{$hash_key2} = $hash_value2; } elsif ($line2 =~ /\S/) { chomp $line2; } } foreach my $hash_key3 (keys %n) { print "$hash_key3\n"; } my @n = (); foreach (keys %n) { push(@n, $_) if exists $s{$_}; #this is where i am having trouble. tried using dumper and #grep didnt + help print "$_\n"; } close FILE1; close FILE2; exit;

help would be appreciated. cheers.

Replies are listed 'Best First'.
Re: Retrieving Key and Value Hashes
by rsx491 (Novice) on Jul 05, 2015 at 11:07 UTC

    You want to print matching values and the first key? The shared/matching keys will be identical, so the key becomes ambiguous.. Try the following code. It uses anonymous hashes and a subroutine.

    use strict; use warnings; use Data::Dumper; my $hash1 = {qw/ name1 ABCD name2 BCD name3 ASCDA name4 AAAAA /}; my $hash2 = {qw/ name1 0.2 name5 0.3 name4 0.0002 name7 0.222 /}; my $hash_result = sub{ my ($hash1,$hash2) = @_; foreach my $key (sort keys $hash1){ print "\'hash1 value: $hash1->{$key}\' AND \'hash2 value: $has +h2->{$key}\' AND shared key: \'$key\'\n" if(defined($hash2->{$key})); } }; $hash_result->($hash1, $hash2);

    My output:
    rsx491@temple:~/Perl/test$ perl hash_compare.pl 'hash1 value: ABCD' AND 'hash2 value: 0.2' AND shared key: 'name1' 'hash1 value: AAAAA' AND 'hash2 value: 0.0002' AND shared key: 'name4'

      Or just return a nested 3rd hash with the matching key/values.

      use strict; use warnings; use Data::Dumper; my $hash1 = {qw/ name1 ABCD name2 BCD name3 ASCDA name4 AAAAA /}; my $hash2 = {qw/ name1 0.2 name5 0.3 name4 0.0002 name7 0.222 /}; my %hash3 = &{sub{ my ($hash1,$hash2,) = @_; my %hash3; foreach my $key (sort keys $hash1){ if(defined($hash2->{$key})){ $hash3{'hash1_values'}{$key} = $hash1->{$key}; $hash3{'hash2_values'}{$key} = $hash2->{$key}; } } return %hash3; }}($hash1, $hash2); print Dumper(sort %hash3);

      My output:
      rsx491@temple:~/Perl/test$ perl hash_compare_return_hash.pl
      $VAR1 = { 'name1' => '0.2', 'name4' => '0.0002' };
      $VAR2 = { 'name1' => 'ABCD', 'name4' => 'AAAAA' };
      $VAR3 = 'hash1_values';
      $VAR4 = 'hash2_values';

Re: Retrieving Key and Value Hashes
by Anonymous Monk on Jul 05, 2015 at 00:03 UTC
    #!/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;

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

        It's a here-document described in perlop.

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

        Probably.

Re: Hashes in two files
by Anonymous Monk on Jul 04, 2015 at 23:41 UTC

    Please show desired output.

      desired output

      name1 ABCD

      name4 AAAAA