At 01:49 PM 2/2/04 -0600, Dennis G. Wicks wrote: >I have a file that I need to sort and currently I am just >sorting it by > > @datalist = sort(@datalist); > >but it will eventually have many more records and many of >them may be quite large, but I only need to sort on the >first six characters which would be faster. Wouldn't it? > >I have looked at perldoc and it shows things like > > @articles = sort {$a <=> $b} @files; > >but I can't figure out how to tell the sort that $a and $b >are the first six characters of @datalist. That is numeric >data BTW.
You start by assuming that $a will refer to one item from list of things being sorted, and that $b will refer to another item that $a is being sorted against. That's what Perl takes it to mean when you refer to $a and $b in a sort function call. To tell Perl to sort on the first 6 chars of the two things being compared, then do it like this: @articles = sort { substr($a,0,6) cmp substr($b,0,6) } @datalist; Note that we're using the string-specific operator 'cmp' rather than the numeric operator <=> because we are comparing strings (the results of substr) rather than the original numbers. (This code assumes each number in your list is at least 6 characters long. If not, you may have a couple extra hoops to jump through.) The general form of the sort function call is sort SUBNAME LIST sort BLOCK LIST sort LIST and the BLOCK or SUBNAME part of the sort call tells Perl how to decide whether, when comparing each pair of things from the list, the first one is less than, equal to, or greater than the second one. For each pair, Perl executes the given BLOCK or SUBNAME, substituting actual values for $a and $b to decide which one comes out on top. HTH. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>