[EMAIL PROTECTED] wrote:
> Hello List,
>  
> I have a datafile in the simple format of:
>  
> |2500|78|Marky|
> |3200|55|Johnny|
> |1988|10|Joey|
>  
> etc....
>  
> What I am trying to do is sort my display out so when the datafile is 
> read in and then display to the screen as its read in it will show the 
> entire entry with the largest first cell first and then descending. So 
> the above would now be displayed as:
>  
> |3200|55|Johnny|
> |2500|78|Marky|
> |1988|10|Joey|
>  
> to the screen but not changing the actuall data file any.
>  
> I tried using the sort function but it isnt sorting by the first number 
> bank? Is sort not the way to do this?

Not sure why you have the extra | on the front and back of each line,
but it should still sort ok for alpha data (reverse sort of course).
If you want a numeric sort, you could try:

use strict;

my @data = (
   '|2500|78|Marky|',
   '|320|55|Johnny|', # shortened 3200 to 320 for testing multi-length #s
   '|1988|10|Joey|',
);

# Schwartzian transform sort - [2] would be [1] if leading | is missing

my @sorted_data = map { $_->[0] }
                  sort { $b->[2] <=> $a->[2] }
                  map { [$_, split /\|/, $_] } @data;

foreach (@sorted_data) {
        print $_, "\n";
}

__END__

See perlfunc man page - sort function
-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to