[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Sorting lines in a file



Hi

First an introduction -- I've been lurking for ages, but this is the 
first time I've piped up. So, hello!

Your method worked fine for me -- the only problem is that you are 
going to be splitting the same line more than once as the sort
progresses. I would use something like...

 my $index = 1;
 my @sorted = map { $_->[1] }
              sort { $a->[0] cmp $b->[0] }
              map { [ (split( /\t/, $_ ))[$index], $_ ] }
              @list;

...that way you split each line once only, and then sort 
by your chosen element.

Not sure why you're not getting the results you need with 
your method though, try as I might I can't get it to fail.

Matt



> All I want to do is sort
> these lines by the various "elements" in each string/list, say by name, or
> course, or date, or whatever.

> Simplified example line:

> name<tab>date<tab>course<tab>tee_position<tab>score<tab>adjusted_score\n

> When I read the file into an array, "@allLines", I have no trouble sorting
> the array by the first element, but I am completely stumped in my efforts
> to sort the lines of the array by the other "elements" in the line.

> I think I know that each line is just a string, with one element, and so
> I've tried everything I can think of to break out the "sub-elements" in
> each line, none of which works:

> ##my umpteenth, and most enterprising attempt
> ##  - using what I thought was a sort function call:
> ####
> sub extractElement	{
> 	$oneLine = shift(@_);
> 	@oneLineAsList = split(/\t/,$oneLine);
> 	$chosenElement = $oneLineAsList[2];
> 	return $chosenElement;
> }
> @sortedOrder = sort { &extractElement($a) cmp &extractElement($b) } @allLines;

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org