in reply to Why is my data structure wrong?

This appears to be a perl bug. You can see in this very stripped down example that the correct hashes are made but they are not pushed into the array(ref or plain) as expected.

use Data::Dumper; my (@data, @col_names, %tmp, $record); @data = <DATA>; chomp @data; @col_names = split ',', shift @data; while(my $line = shift @data) { last unless $line; @tmp{@col_names} = split ',', $line; print 'Inside ', Dumper \%tmp; push @{$record}, \%tmp; } push @{$record}, { 'what ', 'the £$%^& ?' }; print 'Outside ', Dumper $record; print 'Record 0 ', %{$record->[0]}, "\n"; print 'Record 1 ', %{$record->[1]}, "\n"; print 'Record 2 ', %{$record->[2]}, "\n"; print 'Record 3 ', %{$record->[3]}, "\n"; __DATA__ foo,bar 333,aaa 444,bbb 555,ccc __END__ Inside $VAR1 = { 'foo' => '333', 'bar' => 'aaa' }; Inside $VAR1 = { 'foo' => '444', 'bar' => 'bbb' }; Inside $VAR1 = { 'foo' => '555', 'bar' => 'ccc' }; Outside $VAR1 = [ { 'foo' => '555', 'bar' => 'ccc' }, $VAR1->[0], $VAR1->[0], { 'what ' => 'the £$%^& ?' } ]; Record 0 foo555barccc Record 1 foo555barccc Record 2 foo555barccc Record 3 what the £$%^& ?

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Why is my data structure wrong?
by Anonymous Monk on Jun 24, 2002 at 21:08 UTC
    That isn't a bug. Your %tmp hash is declared prior to the loop. You are pushing a reference to the same hash each time through the loop (your $record array ref holds a bunch of pointers to the same thing).