On 17/03/2011 17:22, Chris Stinemetz wrote:
From: Rob Dixon [mailto:[email protected]]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;Thanks Rob, I like you approach. Just curious how can I change the output results to be tab separated and stead of ; separated. Below is what the currentoutput is like with your code: 837;2;975;147 837;2;1025; 837;2;1025;163 837;3;975;174 837;3;975; 837;3;1025;25 837;3;1025;65 840;1;1025; 840;2;975; 841;2;975;149 841;2;975; 842;1;975; 848;2;975; 850;1;975; 850;3;975;
(Please bottom-post your responses to this list, so that extended threads remain comprehensible. Thanks.) I would hope that you could solve that problem for yourself Chris? At the end of the while loop the four fields are joined with semicolons to form the new records, so the join string needs to be changed to a tab character "\t". Within the sort block, these records are split on semicolons to determine the sort order. The split regex needs to be changed to match tabs instead /\t/. Cheers, Rob -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/
