**********
Get rid of @arr out of the while loop, clear @arr every time you get a new key, and you're good to go.
You might also want to change @INST and @KEY to @inst and @key, since by convention capital variable names are constants: perlstyle. (Some people also use capitals to indicate globals, though pelstyle suggests using mixed case to do this.)
#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; my $key; my %hoa; my @arr; while ( <DATA> ) { next if (/^\s+/); if ( /^SET/ ) { my @KEY = split (/[\s:]/,$_); $key = $KEY[2]; @arr = (); # need to do this or get extra elements in second arra +y. Try commenting this out... interesting } elsif (/^\d/) { my @INST = split(/,/,$_); push @arr, [@INST]; } $hoa{$key} = [ @arr]; } print Dumper \%hoa ; __DATA__ SET: SET1 0,100,BOOK 1,150,PENCIL ==== SET: SET2 2,110,ERASER 2,200,PEN 0,220,BLACKBOARD 1,300,CHALK ====
In reply to Re: Fail to update an array in HoAoA
by tphyahoo
in thread Fail to update an array in HoAoA
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |