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

I piping in a file with multiple records and storing these records in a hash after they are split into an array. If I use Data Dumper I can see all of the data I've put in but when I try to loop through the hash to get individual elements I get nothing. The foreach loop I'm using is being ignored completely.


Here is an example of the array record inserted into the hashref and the ineffective loop.
The Data Dumper line just before the loop shows me all the hash data as expected.
Where am I going wrong?
my ($share_hash, %share_hash, @fields); while (<>) { @fields = split(/\|/,$_,-1); $share_hash{$fields[$colhash{res_stay_id}]} = [@fields]; print Dumper(%share_hash); foreach my $share_item (keys %$share_hash) { my $row = $share_hash->{$share_item}; print STDOUT join("|", @$row), "\n"; } }

Sample input
X|156853|2012-09-11 10:10:51|2012-09-10|LQ|NVCORP|0570|2012-09-06|2012-09-11|2012-09-06||1|1|0|USD|47.50|237.50|.00|DDR|N|93446|PASO ROBLES|CA|||N||N|Contract/Crew|CC2|101|101|Walk In|WERNER|158167|156853 X|156855|2012-09-11 10:10:51|2012-09-10|LQ|NVCORP|0570|2012-09-06|2012-09-10|2012-09-06||1|1|0|USD|47.50|190.00|95.00|DDR|N|93230|HANFORD|CA|||N||N|Contract/Crew|CC2|101|101|Walk In|WERNER|158167|156853 X|157155|2012-09-11 10:10:51|2012-09-10|LQ|NVCORP|0570|2012-09-10|2012-09-11|2012-09-10||1|1|0|USD|47.50|47.50|.00|DDR|N|90008|LOS ANGELES|CA|||N||N|Contract/Crew|CC2|100|100|Property Direct|WERNER|158167|156853 X|156854|2012-09-11 10:10:51|2012-09-10|LQ|NVCORP|0570|2012-09-06|2012-09-10|2012-09-06||1|1|0|USD|47.50|190.00|95.00|DDR|N|90008|LOS ANGELES|CA|||N||N|Contract/Crew|CC2|101|101|Walk In|WERNER|158168|156854 X|156861|2012-09-11 10:10:51|2012-09-10|LQ|NVCORP|0570|2012-09-06|2012-09-10|2012-09-06||1|1|0|USD|47.50|237.50|95.00|DDR|N|97007|ALOHA|OR|||N||N|Contract/Crew|CC2|100|100|Property Direct|WERNER|158168|156854 X|156934|2012-09-11 10:10:51|2012-09-10|LQ|NVCORP|0570|2012-09-07|2012-09-10|2012-09-07||1|1|0|USD|47.50|95.00|142.50|KP|N|84128|WEST VALLEY|UT|||N||N|Contract/Crew|CC2|101|101|Walk In|WERNER|158248|156934

Thanks for the help. After clarifying the variables I was using I was able to get the output I wanted. Unfortunately I work with some folks who like to make their Perl code as confusing as possible so they name their variable all the same.

Replies are listed 'Best First'.
Re: hashes hash refs and arrays
by toolic (Bishop) on Sep 12, 2012 at 20:18 UTC
    my ( $share_hash, %share_hash, @fields );
    You have 2 variables with the same name. To ease the confusion, rename $share_hash as $share_hash_ref

    Some other things to try:

    • Output will be easier to read if you pass a ref to Dumper: Dumper(\%share_hash)
    • Show use a few lines of your input file.
    • Basic debugging checklist
Re: hashes hash refs and arrays
by aitap (Curate) on Sep 12, 2012 at 20:45 UTC
    $share_hash{$fields[$colhash{res_stay_id}]} = [@fields];
    This fills the %share_hash hash.
    foreach my $share_item (keys %$share_hash) { my $row = $share_hash->{$share_item};
    This tries to work with $share_hash scalar variable (which is different from %share_hash) as a hash reference and should probably trow some errors/warnings.
    Use keys %share_hash and $share_hash{$share_item} instead.
    Sorry if my advice was wrong.