On Mon, Jun 20, 2016 at 10:40 AM, Shlomi Fish <shlo...@shlomifish.org> wrote:
> Hi Chris,
>
> On Mon, 20 Jun 2016 11:28:57 -0600
> Chris Fedde <ch...@fedde.us> wrote:
>
>> Kenneth,
>>
>> Below the cut is my example implementation as I understand your
>> requirements.
>> Note that the "compare" routine uses $a and $b which are "special" to perl
>> sort routines.
>> Also the compare routine is written for obviousness rather than for brevity
>> or elegance.
>>
>> The return from compare illustrates Shalomi Fish's point about using the
>> "||" operator to compose sort fields.
>> Descending numeric order is done by reversing the comparison on that sub
>> field.
>>
>
> Thanks for the illustrative example! I was too lazy to write one.
>
> Regards,
>
>         Shlomi Fish
>
>
>> chris
>>
>> ----- cut -----
>> #!/usr/env/bin perl
>>
>> use strict;
>> use warnings;
>>
>> my @x = <DATA>;
>>
>> sub compare {
>>     my @a = split(/\t/, $a);
>>     my @b = split(/\t/, $b);
>>     return $b[0] <=> $a[0] || $a[1] cmp $b[1]
>> }
>>
>> print for (sort {compare} @x);
>>
>> __DATA__
>> 9500       ohzaew
>> 5300       dohpha
>> 0700       liemah
>> 1700       phuhei
>> 0200       phuowo
>> 1300       ojaeng
>> 3900       aebaat
>> 4200       dohgha
>> 4200       aiyiej
>> 6300       ojaeng
>> 1600       haequa
>> 3100       hupiez
>> 3200       ahrieb
>> 3600       ohzaew
>> 5300       queebe
>> 2000       oeyael
>> 0200       hahwoo
>> 9900       shahye
>> 9300       johhir
>> 6400       shahye
>> 4500       ohfici
>> 5500       ahngoh
>> 7300       aibove
>> 8200       ahrieb
>> 9100       ohzaew
>> 3100       ohzaew
>> 2800       gahnoh
>> 0800       aedeng
>> 8400       oowaih
>> 0300       vouroh
>> 1400       shahye
>> 0500       ciejee
>> 0500       uanahp
>> 2100       ophuum
>> 1500       aideev
>> 6900       aegeuw
>> 6300       haequa
>> 9300       queebe
>> 5400       reogai
>> 5000       ophuum
>> 1700       aebaat
>> 1600       eshida
>> 3700       beidae
>> 5200       quieki
>> 6800       eashoo
>> 6800       ohweba
>> 2300       apahqu
>> 8100       ahghee
>> 6700       jooxoj
>> 3500       yeiboo
>> 2800       chuema

Thank you so much!

Now I've got to study this and experiment until it becomes natural.

Meanwhile, I studied what was on line 43 of the perldoc sort page and
decided to try to use that and modify it.

I took out the equals sign and then I took out the second stanza.

It seems to work:

@sorted_summary_detail = sort {($b =~ /(\d+)/)[0] <=> ($a =~
/(\d+)/)[0]} @summary_detail;

Again, thank you; I have to study this.

Ken Wolcott

-- 
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