On 05/17/2012 04:52 PM, Shawn H Corey wrote:
On 12-05-17 03:36 PM, Chris Stinemetz wrote:
I would like to sort the array by
$fields[0],$fields[1],$fields[2],$fields[3],$fields[4],$fields[5] in
ascending order starting witht he first element before I print the
array.
Do you want the fields sorted or do you want records sorted? If you want
the records sorted, try:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;
# configuration
my $fileOut = "testOut.txt";
my $field_separator = quotemeta( ';' );
my $ICELL_INDEX = 9;
my $ISECT_INDEX = 10;
my $ICHAN_INDEX = 27;
my $CFC_INDEX = 36;
my $CFCQ_INDEX = 37;
my $RTD_INDEX = 40;
# main
open my $out_fh, '>', $fileOut or die "ERROR opening $fileOut: $!";
my @fields;
while( <> ) {
next unless /$field_separator/;
chomp;
# split by separator
my @data = split /$field_separator/;
# load variables
my ( $Icell, $Isect, $Ichan, $cfc, $cfcq, $rtd )
= @data[ $ICELL_INDEX, $ISECT_INDEX, $ICHAN_INDEX, $CFC_INDEX,
$CFCQ_INDEX, $RTD_INDEX, ];
if( $Icell >= 1
&& $Isect >= 1
&& $Ichan >= 1
&& $cfc >= 1
&& $cfcq >= 1
&& $rtd >= 1
){
$rtd = sprintf "%.2f", $rtd / 8 / 6.6 / 2;
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;
i wasn't going to post directly as thought it might be too complex for
the OP, but seeing this made me change my mind. (not that your code is
overly complex, 5 keys deep is just complex!). take a look at
Sort::Maker on cpan. you can describe your sort keys in a more natural
way, ordering is builtin (the order of the keys) and it is much less
code. also you can have it generate perl code for 4 different sort
styles which is very helpful in learning how to sort in perl.
uri
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/