Trimmed to the parts that use and modify @data:
my @data; while ($running) { unless ($nfound) { ... } else { @data = split /\n/, $rawdata; } for (my $i=0; $i<=$#data; ++$i) { printf "data[$i]=%s\n", $data[$i]; } foreach (@data,) { print "line=".$_."\n"; if (...) { ... push @data, [ $name, $rrate, $wrate, $total ]; } } my @sorted_data = sort { $a->[3] <=> $b->[3] } @data; ... @data = undef; }
The outputs of the loops differ because you modify @data in the middle of it all.
Don't change the array over which you are iterating, and don't use the same variable for two different purposes.
Fix:
while ($running) { my @data; unless ($nfound) { ... } else { @data = split /\n/, $rawdata; } for (my $i=0; $i<=$#data; ++$i) { printf "data[$i]=%s\n", $data[$i]; } my @parsed_data; foreach (@data,) { print "line=".$_."\n"; if (...) { ... push @parsed_data, [ $name, $rrate, $wrate, $total ]; } } my @sorted_data = sort { $a->[3] <=> $b->[3] } @parsed_data; ... }
Note that I got rid of the @data = undef; by properly scoping my vars.
Update: Added more of original code.
Update: Added solution.
In reply to Re: foreach(@input_lines) giving array ref not scalar? Confusion
by ikegami
in thread foreach(@input_lines) giving array ref not scalar? Confusion
by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |