in reply to Re: how to use the values of the first hash to be the keys for the second hash
in thread how to use the values of the first hash to be the keys for the second hash

thank you so much.

when I tried to print the %newhash:

#!/usr/bin/perl use warnings; use strict; use Data::Dump qw[ pp ]; #subroutine: read in hash tables my $hash1=shift; open (my $fh1, "<",$hash1) or die "$!"; my %h1=(); while (<$fh1>) { chomp; my ($k1,$v1)=split /\t/; $h1{$k1}=$v1; } my $hash2=shift; open (my $fh2, "<",$hash2) or die "$!"; my %h2=(); while (<$fh2>) { chomp; my ($k2,$v2)=split /\t/; $h2{$k2}=$v2; } my %newhash = map { $_ => join ',', @h2{ split ',', $h1{ $_ } }; } keys %h1; while (my ($k, $v) = each %newhash) { print $k, "\t@{$newhash{$k}}\n"; }

it says "Can't use string ("val3") as an ARRAY ref while "strict refs" in use at line 34, <$fh2> line 3.'"

could you explain to me why?,/p>

  • Comment on Re^2: how to use the values of the first hash to be the keys for the second hash
  • Download Code

Replies are listed 'Best First'.
Re^3: how to use the values of the first hash to be the keys for the second hash
by BrowserUk (Patriarch) on Sep 07, 2012 at 22:08 UTC

    Have you tried dumping the two input hashes after you've populated them?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      may sounds stupid, but I don't really understand how to "dumping the two input hashes after populated them"

        You included the use Data::Dump qw[ pp ]; from my code into yours;

        but then you threw away the pp \%newhash; which I used to produce the output:

        C:\test>junk { key1 => "val5,val6,val7,val8,val9", key2 => "val3" }

        If, after populating your two hashes: %h1 & %h2, you used the same tool to dump them to the screen before trying to use them:

        pp \%h1; pp \%h2;

        You could check whether you have populated them correctly.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

        div