develooper Front page | perl.perl6.users | Postings from July 2020

Re: Raku version of "The top 10 tricks of Perl one-liners" ?!?

Thread Previous | Thread Next
From:
Elizabeth Mattijsen
Date:
July 22, 2020 21:19
Subject:
Re: Raku version of "The top 10 tricks of Perl one-liners" ?!?
Message ID:
BAD306B1-B451-43A4-A9D9-D6C1EDECA61A@dijkmat.nl
Larry,

good to see you here!

Of course, I have some comments on your suggestions  :-)


> On 22 Jul 2020, at 21:14, Larry Wall <larry@wall.org> wrote:
>>   Trick #5: -a
>> 
>>       -a turns on autosplit mode – perl will automatically split input
>>       lines on whitespace into the @F array. If you ever run into any advice
>>       that accidentally escaped from 1980 telling you to use awk because
>>       it automatically splits lines into fields, this is how you use perl
>>       to do the same thing without learning another, even worse, language.
>> 
>>       As an example, you could print a list of files along with their link
>>       counts using
>> 
>>       ls -l | perl -lane 'print "$F[7] $F[1]"'
> This feature was always a bit suspect because it hard-wired a particular
> name.  You don't even need a weird name in Raku:
> 
>     ls -l | raku -ne 'say "$_[7] $_[1]" given .words'

ls -l | raku -ne 'put .words[7,1]'

"put" stringifies a slice with a space in between elements.


>>   Trick #6: -F
>> 
>>       -F is used in conjunction with -a, to choose the delimiter on
>>       which to split lines. To print every user in /etc/passwd (which is
>>       colon-separated with the user in the first column), we could do:
>> 
>>       perl -F: -lane 'print $F[0]' /etc/passwd
> 
> Again, we felt this switch wasn't really pulling it's weight, so we pulled it
> in favor of explicit split or comb:
> 
>     raku -ne 'say $_[0] given .split(":")' /etc/passwd

raku -ne 'say .split(":").head' /etc/passwd

"head" takes the first element of a Seq *without* needing to cache it, making it about twice as fast.

Liz
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