On 16/03/2011 14:58, Chris Stinemetz wrote:
> Shawn H Corey wrote:
>> On 11-03-16 09:26 AM, Chris Stinemetz wrote:
>>>
>>> I would like to print results in ascending order starting with $cell, 
>>> $sect, and finally $carr.
>> 
>> You would need to store the data in a large array, then sort it.
>> 
>> # untested due lack of data
>> my @array = ();
>> 
>> while (<>)
>> {
>>      chomp;
>>      if (/;/)
>>      {
>>        my @data = split /;/;
>>        my($cell,$sect,$carr,$RTD) = 
>> ($data[31],$data[32],$data[38],$data[261]);
>>        push @array, {
>>          cell =>  $cell,
>>          sect =>  $sect,
>>          carr =>  $carr,
>>          RTD  =>  $RTD,
>>        };
>>     }
>> }
>> 
>> my @sorted = map { $_->[0] }
>>                sort {
>>                   $a->[1]<=>  $b->[1]
>>                   || $a->[2]<=>  $b->[2]
>>                   || $a->[3]<=>  $b->[3]
>>                }
>>                map { [ $_, $_->{cell}, $_->{sect}, $_->{carr} ]}
>>                @array;
> 
> Where would I place print to see the results for validation?

If you are uncomfortable with arrays of hash references, it may be
better for you to create an array of records containing just the four
fields you are interested in, and then sort that. Such a program is
shown below. It is tested and seems to work fine.

This method is inefficient, as the records are split every time sort
needs to compare a pair of them, but unless you are dealing with vast
amounts of data this will not be a problem. It is very important that
you understand the code you write, so that you can answer questions on
it and maintain it.

HTH,

Rob

use strict;
use warnings;

my @array;

while (<DATA>) {
  next unless /;/;
  chomp;
  my @data = ( split /;/ )[31,32,38,261];
  push @array, join ';', @data;
}

@array = sort {
  my @aa = split /;/, $a;
  my @bb = split /;/, $b;
  $aa[0] <=> $bb[0] or
  $aa[1] <=> $bb[1] or
  $aa[2] <=> $bb[2];
} @array;

print "$_\n" foreach @array;


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to