On 17/05/2012 23:19, Shawn H Corey wrote: > On 12-05-17 05:24 PM, Chris Stinemetz wrote: >> >> push(@fields, $Icell,$Isect,$Ichan,$cfc,$cfcq,$rtd); > > # push an anonymous array for each record > push @fields, [ $Icell,$Isect,$Ichan,$cfc,$cfcq,$rtd ]; > >> } >> } >> >> >> my @sorted_fields = sort { >> $a->[0]<=> $b->[0] >> || $a->[1]<=> $b->[1] >> || $a->[2]<=> $b->[2] >> || $a->[3]<=> $b->[3] >> || $a->[4]<=> $b->[4] >> || $a->[5]<=> $b->[5] >> } @fields; >> >> print Dumper \@fields;
I must point out that throughout this thread the array @sorted_fields has been created and then discarded. The output should display @sorted_fields instead of @fields. I also believe the data should be stored as an array of hashes, which simplifies the code substantially. The program belowdoes what is required, but beware that it is untested beyond compilation. HTH, Rob use strict; use warnings; use Data::Dumper; my $fileOut = "testOut.txt"; open my $out, '>', $fileOut or die "ERROR opening $fileOut: $!"; my @data; while (<>) { next unless /;/; chomp; my %fields; @fields{qw/ Icell Isect Ichan cfc cfcq rtd /} = (split /;/, $_, -1)[9,10,27,36,37,40]; next if grep $_ < 1, values %fields; $fields{rtd} = sprintf "%.2f", $fields{rtd} / 8 / 6.6 / 2; push @data, \%fields; } sub by_fields { for (qw/ Icell Isect Ichan cfc cfcq rtd /) { my $cmp = $a->{$_} <=> $b->{$_}; return $cmp if $cmp; } return 0; } my @sorted = sort by_fields @data; print Dumper \@sorted; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/