Chris Rogers wrote:
> 
> I am getting a syntax error that I can't seem to resolve.
> 
> The single dimensioned array to be sorted consists of rows that look like
> this:
> 
> 105|1156|1|576|VIFAN|1|576|10/24/97|0426|0048|0050|0|0|0|0|01/01/00|12/21/99
> |LCI
> 
> Each record is pipe "|" delimited and the average array holds from 500 to
> 2500 records
> 
> Here is a snippet of code that works:
> 
> sub sortarray
> {
>     my $primary = shift; #primary sort field
>     my $ptype = shift; #0=numeric 1=string
>     my $secondary = shift; #secondary sort field
>     my $stype = shift; #0=numeric 1=string
>     my $terciary = shift; #terciary sort field
>     my $ttype = shift; #0=numeric 1=string
>     my @data = @_;
> 
>         my @temparray = map {$_->[0] }
>         sort {
>                 @a_fields = @$a[1..$#$a];
>                 @b_fields = @$b[1..$#$b];
> 
>                 $b_fields[$primary] cmp $a_fields[$primary]
>                     ||
>                 $b_fields[$secondary] cmp $a_fields[$secondary]
>                     ||
>                 $b_fields[$terciary] cmp $a_fields[$terciary]
>              }
>              map { [$_, split /\|/] } @data;
>         return @temparray;
>     }
> }
> 
> Here's the problem:  I'm trying to change the routine so that it can handle
> alpha sorting and numeric sorting on the fly.  I have tried many different
> ways to make this work but I just can't seem to get it right.  The following
> snippet is the best I've come up with yet but it still produces a syntax
> error:
> 
> [snip]
> 
> Any help would be greatly appreciated.


It looks like the numbers in your data are not floating point so
this should work although I haven't tested it.  It converts integers
to strings so that you only have to use the cmp operator.

sub sortarray {
    my $primary   = shift; # primary sort field
    my $ptype     = shift; # 0=numeric 1=string
    my $secondary = shift; # secondary sort field
    my $stype     = shift; # 0=numeric 1=string
    my $terciary  = shift; # terciary sort field
    my $ttype     = shift; # 0=numeric 1=string
    my @data      = @_;

    return map  { $_->[3] }
           sort { $b->[0] cmp $a->[0] || $b->[1] cmp $a->[1] || $b->[2]
cmp $a->[2] }
           map  { my @a = (split/\|/)[$primary,$secondary,$terciary];
                  $a[0] = pack( 'l', $a[0] ) unless $ptype;
                  $a[1] = pack( 'l', $a[1] ) unless $stype;
                  $a[2] = pack( 'l', $a[2] ) unless $ttype;
                  [ @a, $_ ]
                } @data;
    }
}



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to