develooper Front page | perl.perl6.users | Postings from June 2018

Re: need sort help

Thread Previous | Thread Next
From:
Brent Laabs
Date:
June 9, 2018 04:44
Subject:
Re: need sort help
Message ID:
CAAPDpBxVk_rsNz9Mr44Sd4fs-JKuyqV6GJDPo-Jv-zHW0caaGQ@mail.gmail.com
It isn't in any variable, @x.sort got called in sink context.  You'll have
to assign it if you want, maybe use @x.=sort ?

I just wanted to give an example of how to solve the problem, not
necessarily a working solution.  First of all, I did thinko the solution --
element 6 is [5]...  and we don't really need to check the last bits of
time if we're going to do cmp anyway...

@x.=sort: {
    my $a = $^x ~~ m:g/\d+/;  # extract digit portions in to match object
for each parameter to sort
    my $b = $^y ~~ m:g/\d+/;  # ditto
    $a[5].defined cmp $b[5].defined   # this part makes sure we're actually
looking at a timestamp with 6 digit groups
    ??
      $a[3] cmp $b[3]   # year
    || $a[1] cmp $b[1]  # month
    || $a[2] cmp $b[2]  # day
    || $x cmp $y          # sort whatever's left
    !! $x cmp $y      # doesn't have a date stamp, then just string compare
};

The part about placeholders being declarations I didn't know, so hey, I'm
glad I participated.


On Fri, Jun 8, 2018 at 9:37 PM, ToddAndMargo <ToddAndMargo@zoho.com> wrote:

> On 06/08/2018 09:25 PM, Brandon Allbery wrote:
>
>> @x.sort: {
>>     my $a = $^x ~~ m:g/\d+/;
>>     my $b = $^y ~~ m:g/\d+/;
>>     $a[6].defined cmp $b[6].defined
>>     ??
>>       $a[3] cmp $b[3]
>>     || $a[1] cmp $b[1]
>>     || $a[2] cmp $b[2]
>>     || $a[4] cmp $b[4]
>>     || $a[5] cmp $b[5]
>>     || $a[6] cmp $b[6]
>>     || $x cmp $y
>>     !! $x cmp $y
>> };
>>
>> This is what I sent before, minus the leading "> " that I'd neglected to
>> remove.
>>
>> Please note the second line does NOT say "my $a = $a ~~ m:g/\d+/;", which
>> is what you had and what produced the error you pasted. I don't know where
>> you got that. Likewise the third line with $b and $^y (where again you had
>> $b instead).
>>
>> On Sat, Jun 9, 2018 at 12:19 AM ToddAndMargo <ToddAndMargo@zoho.com
>> <mailto:ToddAndMargo@zoho.com>> wrote:
>>
>>     On 06/08/2018 09:16 PM, Brandon Allbery wrote:
>>      > You didn't use what I sent. It's $^x and $^y, not $a and $b.
>>
>>     I am lost.  Would you mind writing it out for me?
>>
>
> $ ls | perl6 -e 'my @x=slurp(); say @x.sort: {my $a = $^x ~~ m:g/\d+/;my
> $b = $^y ~~ m:g/\d+/; $a[6].defined cmp $b[6].defined ??  $a[3] cmp $b[3]
> || $a[1] cmp $b[1]   || $a[2] cmp $b[2]  || $a[4] cmp $b[4]  || $a[5] cmp
> $b[5]  || $a[6] cmp $b[6]  || $x cmp $y !! $x cmp $y};'
> (cimtrak.log.06-08-2018_16:07:39.zip
> cimtrak.log.06-08-2018_17:07:39.zip
> cimtrak.log.07-08-2018_06:07:39.zip
> cimtrak.log.07-08-2018_16:07:39.zip
> cimtrak.log.12-08-2016_06:07:39.zip
> )
>
>
> :'(
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About