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

I don't seem to understand what is going on here with "dump" module. I expect to see the HASH but only returns the last key, value...why?what am I missing? please help me understand.

uno = uno due = dos tre = tres quattro = quatro cinque = cinco
use strict; use warnings; use Data::Dump qw(dump); my %hash; open my $in, '<',"./test_data.txt" or die ("can't open the file:$!\n") +; my $data; while (<$in>){ chomp; %hash= split (/\s*=\s/); #dump \%hash; #checking on what's being assigned to hash } close $in; #print "this is the data\n"; dump \%hash;

Replies are listed 'Best First'.
Re: why aren't I see the hash while using dump?
by FreeBeerReekingMonk (Deacon) on Jun 01, 2015 at 21:26 UTC
    while (<$in>) { chomp; my @data = split(/\s*=\s*/); $hash{$data[0]} = $data[1]; };

    edit: more readable:

    while (<$in>) { chomp; my ($key, $value) = split(/\s*=\s*/); $hash{$key} = $value; };
      grrr, I see what you did! thanks! it wasn't dump...still figuring this out. thanks again. can I use "push" to get keys and values?

        You _can_ typecast, however, like so (array to hash key value pairs)

        use Data::Dumper; use warnings; use strict; open FOO, "test_data.txt" or die $!; my @A; while(<FOO>){ chomp; push @A, split /\s*=\s*/, $_, 2 } my %H = @A; print Dumper(\%H);

        Included a maximum of 2 elements in the split, as suggested by An.Monk.

        push/pop and shift/unshift are for arrays only, not hashes.

      2nd edit is alot more clearer for me. I totally understand what I didn't do! thanks!
      now, moving on to do more interesting stuff with hashes...I am having problems with new $k and $v to already existing read in hash . can you help debug this piece? where I attempt to check in data2 with already existing hash{key}=$value?? I added this piece to the code which previously loaded the hash from 1st file.
      <data> uno = uno due = dos tre = tres quattro = quatro cinque = cinco <\data> <data2> uno => tree due => dos tre => tres quattro => cinco cinque => cinco sei => seis <\data2>
      use strict; use warnings; use Data::Dump qw(dump); use Storable; #don't know what this does yet. my %hash; open my $in, '<',"./test_data.txt" or die ("can't open the file:$!\n") +; while (<$in>){ chomp; my ($key, $value)= split (/\s*=\s/); $hash{$key}=$value; } close $in; ## <new code> open my $in2,'<',"./test_data2.txt" or die ("can't open file : $!\n"); open my $out ,'>' ,"./test_data_out.txt" or die "can't open the file f +or write:$1\n"; while (<$in2>){ chomp; my ($key,$value) = split (/\s*=\s/); #splits row into 2 col #will check if new key,value matches then print to another file for f +un. if exists $hash{$keys}{ dump \$out; } }

        hopefully, I formatted this correctly. stuck while reading in the second keys and values of 2nd file and checking against the existing hash key and value can you help debug this piece?

        uno = uno due = dos tre = tres quattro = quatro cinque = cinco
        uno = tree due = dos tre = tres quattro = cinco cinque = cinco sei = seis
        use strict; use warnings; use Data::Dump qw(dump); use Storable; #don't know how to use this to test yet. my %hash; open my $in, '<',"./test_data.txt" or die "can't open the fi +le:$!\n"; while (<$in>){ chomp; my ($key, $value)= split (/\s*=\s/); $hash{$key}=$value; #it's clear to me now } close $in; #will check against %hash with new keys and values #opening files for read and write open my $in2,'<',"./test_data2.txt" or die "can't open file : $!\n"; open my $out ,'>' ,"./test_data_out.txt" or die "can't open the file f +or write:$!\n"; while (<$in2>){ chomp; my ($key,$value) = split (/\s*=\s/); #splits row into 2 col if exists $hash{$keys}{ dump $out \%hash; } }

        ... <data>...<\data> ... <data2>...<\data2> ... <code>...<\code> ...

        Maybe you didn't notice it but
        Use:  <p> text here (a paragraph) </p>
        and:  <code> code here </code>
        to format your post; it's PerlMonks-approved HTML":

        Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!

Re: why aren't I see the hash while using dump?
by stevieb (Canon) on Jun 01, 2015 at 21:34 UTC

    Please put your code and input into <code></code> tags, as it makes it much more difficult to understand where the problem is when you don't.

      oh, cool. I will do next time...thanks.