Re: another one liner
> On Aug 4, 2017, at 9:12 PM, ToddAndMargowrote: > > Hi All, > > How would you convert this one liner over to a Perl 6 one > liner with a pipe? > > ifconfig | grep flags | awk '{print $1}' | sort | sed -n 2,2p | sed -e 's/://‘ —snip— First attempt; straight translation: ifconfig | perl6 -e 'say lines().grep({/flags/}).map({.words.[0].subst(/":"$/, "")}).sort.[1]' P6-ish version: ifconfig | perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ }).sort[1]' — Hope this helps, Bruce Gray (Util of PerlMonks)
Re: another one liner
On 08/05/2017 12:34 AM, Elizabeth Mattijsen wrote: On 5 Aug 2017, at 09:21, ToddAndMargowrote: On 08/04/2017 08:43 PM, Bruce Gray wrote: P6-ish version: ifconfig | perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ }).sort[1]' Would some kind person please tell me what is going on here? say the result of reading all lines of STDIN mapping them to the stringified match (~) of the first ($0) non-whitespace (\S+) characters starting at the beginning of the string (^) if they’re followed by “: flags=“ sorting these strings (.sort) taking the second element of the sorted list ([1] Thank you! I will have to read it over several times!
Re: another one liner
> On 5 Aug 2017, at 09:21, ToddAndMargowrote: > > On 08/04/2017 08:43 PM, Bruce Gray wrote: >> P6-ish version: >> ifconfig | perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ }).sort[1]' > > Would some kind person please tell me what is going on here? say the result of reading all lines of STDIN mapping them to the stringified match (~) of the first ($0) non-whitespace (\S+) characters starting at the beginning of the string (^) if they’re followed by “: flags=“ sorting these strings (.sort) taking the second element of the sorted list ([1]
Re: another one liner
> On 5 Aug 2017, at 08:50, Sean McAfeewrote: > > On Fri, Aug 4, 2017 at 10:18 PM, ToddAndMargo wrote: > On 08/04/2017 08:43 PM, Bruce Gray wrote: > > P6-ish version: > ifconfig | perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ }).sort[1]' > > > Wait a second. How does map skip input elements like that? > > > map { $_ if $_ %% 2 }, 1..10 > (2 4 6 8 10) > > > 1 if 1 %% 2 > () > > But: > > > map { $_ %% 2 ?? $_ !! () }, 1..10 > (() 2 () 4 () 6 () 8 () 10) Because a failing “if" returns the empty Slip (aka Empty): $ 6 'dd do if 0 { }’ Empty Now isn’t that convenient :-) Liz
Re: another one liner
On Sat, Aug 5, 2017 at 2:50 AM, Sean McAfeewrote: > On Fri, Aug 4, 2017 at 10:18 PM, ToddAndMargo > wrote: > >> On 08/04/2017 08:43 PM, Bruce Gray wrote: >> >>> >>> P6-ish version: >>> ifconfig | perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ >>> }).sort[1]' >>> >> >> > Wait a second. How does map skip input elements like that? > > > map { $_ if $_ %% 2 }, 1..10 > (2 4 6 8 10) > > > 1 if 1 %% 2 > () > > But: > > > map { $_ %% 2 ?? $_ !! () }, 1..10 > (() 2 () 4 () 6 () 8 () 10) > Perl 5 doesn't do nested lists (unless you use arrayrefs) so () flattens away. In Perl 6, to get a similar effect you produce Nil. As Nil is the default value in that first block, it gets produced if the condition fails. -- brandon s allbery kf8nh sine nomine associates allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
Re: another one liner
On Fri, Aug 4, 2017 at 10:18 PM, ToddAndMargowrote: > On 08/04/2017 08:43 PM, Bruce Gray wrote: > >> >> P6-ish version: >> ifconfig | perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ >> }).sort[1]' >> > > Wait a second. How does map skip input elements like that? > map { $_ if $_ %% 2 }, 1..10 (2 4 6 8 10) > 1 if 1 %% 2 () But: > map { $_ %% 2 ?? $_ !! () }, 1..10 (() 2 () 4 () 6 () 8 () 10)
Re: another one liner
On 08/04/2017 08:43 PM, Bruce Gray wrote: On Aug 4, 2017, at 9:12 PM, ToddAndMargowrote: Hi All, How would you convert this one liner over to a Perl 6 one liner with a pipe? ifconfig | grep flags | awk '{print $1}' | sort | sed -n 2,2p | sed -e 's/://‘ —snip— First attempt; straight translation: ifconfig | perl6 -e 'say lines().grep({/flags/}).map({.words.[0].subst(/":"$/, "")}).sort.[1]' P6-ish version: ifconfig | perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ }).sort[1]' $ ifconfig | grep flags | awk '{print $1}' | sort | sed -n 2,2p | sed -e 's/://' enp6s0 $ ifconfig | /usr/bin/perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ }).sort[1]' enp6s0 $ ifconfig | /usr/bin/perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ }).sort[2]' enp7s0 Thank you! I have no idea what you just did. -T
Re: another one liner
On 08/04/2017 08:14 PM, Timo Paulssen wrote: ifconfig | perl6 -e 'lines.grep("flags").map(*.words[0]).sort[1].chop' this should do the trick. i'm not sure if 2,2p is meant to "output just the second result" and if it's okay to just unconditionally remove the last character. hth - Timo 2,2p means to print only the second line $ ifconfig | /usr/bin/perl6 -e 'lines.grep("flags").map(*.words[0]).sort[1].chop' Use of Nil in string context in block at -e line 1 :'( -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~