in reply to why aren't I see the hash while using dump?

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; };

Replies are listed 'Best First'.
Re^2: why aren't I see the hash while using dump?
by perlynewby (Scribe) on Jun 01, 2015 at 21:37 UTC
    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.

        You can, and it's a good thing to know sometimes, but in this case it'll cause strange things to happen if there's more than one = on a line. Your original suggestion was better, perhaps with the enhancement of giving the split a limit, e.g. split /\s*=\s*/, $_, 2.

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

Re^2: why aren't I see the hash while using dump?
by perlynewby (Scribe) on Jun 01, 2015 at 21:43 UTC
    2nd edit is alot more clearer for me. I totally understand what I didn't do! thanks!
Re^2: why aren't I see the hash while using dump?
by perlynewby (Scribe) on Jun 02, 2015 at 01:25 UTC
    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; } }

      ... <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!

        I apologize for not getting the format correct. I'm working/learning how to communicate here. also, how to update a follow up question? I tried but I am Unable. thanks!

      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; } }


        my ($key,$value) = split (/\s*=\s/);
        if exists $hash{$keys}{
           dump $out \%hash;
        }


        Are you using use strict; use warnings; ? That would immediately tell you you are using a variable that has not been declared previously.
        Please use that from now on...