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

[MacPerl] Sorting lines in a file



I'm a [Mac]Perl novice, and I have reached the end of my rope with a
problem that I hope has a simple solution:

I've created a text file in which each line is a tab delimited string/list
recording information about a round of golf.  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;
####

This method accomplishes nothing  ... both @allLines and @sortedOrder have
the same contents, in the same order.  Consistent with this do-nothing
result, after the sub &extractElement has done its thing, both $oneLine and
$chosenElement are empty.   There seems to be no doubt that I am going
about this the wrong way.

I won't burden this message with the numerous other methods and workarounds
I've attempted, since none of those worked either, and I'm holding out hope
that there is a simple/obvious/trivial solution that one of Mac-friendly
list gurus will point out without too much opprobrium.

In any event, thanks for any help, and also thanks after the fact for all
of the other helpful information I've gleaned from this list.

John Echols
<defender@pianosa.com>
<www.pianosa.com>



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