in reply to Re: How to perform different sorts on multiple sections of items in the same array
in thread How to perform different sorts on multiple sections of items in the same array
Updated: added missing declaration per trippledubs's report below.
A somewhat more optimal version that avoids multiple greps and re-testing of some conditions:
#! perl -slw use strict; my @data = qw[ alpha bravo charlie delta east echo exit foxtrot golf hotel india +juliet kilo lima mike november oscar papa quebec romeo sierra tango uniform victor whisk +ey xray yankee zulu ]; my @sortedData; my( @rindex, @eindex, @oindex ); push @{ $data[ $_ ] =~ m[r] ? \@rindex : $data[ $_ ] =~ m[^e] ? \@eind +ex : \@oindex }, $_ for 0 .. $#data; my @rsorted = sort{ $data[ $a ] cmp $data[ $b +] } @rindex; my @esorted = sort{ substr( $data[ $a ], 2, 1 ) cmp substr( $data[ $b +], 2, 1 ) } @eindex; my @osorted = sort{ substr( $data[ $b ], -1 ) cmp substr( $data[ $a +], -1 ) } @oindex; @sortedData[ @rindex, @eindex, @oindex ] = @data[ @rsorted, @esorted, +@osorted ]; print for @sortedData;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to perform different sorts on multiple sections of items in the same array (optimised)
by trippledubs (Deacon) on Dec 30, 2014 at 05:25 UTC | |
by Athanasius (Archbishop) on Dec 30, 2014 at 10:11 UTC | |
by BrowserUk (Patriarch) on Dec 30, 2014 at 10:20 UTC |