Re: another one liner

2017-08-07 Thread Bruce Gray

> On Aug 4, 2017, at 9:12 PM, ToddAndMargo  wrote:
> 
> 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

2017-08-05 Thread ToddAndMargo

On 08/05/2017 12:34 AM, Elizabeth Mattijsen wrote:



On 5 Aug 2017, at 09:21, 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]'


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

2017-08-05 Thread Elizabeth Mattijsen

> On 5 Aug 2017, at 09:21, 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]'
> 
> 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

2017-08-05 Thread Elizabeth Mattijsen
> On 5 Aug 2017, at 08:50, Sean McAfee  wrote:
> 
> 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

2017-08-05 Thread Brandon Allbery
On Sat, Aug 5, 2017 at 2:50 AM, Sean McAfee  wrote:

> 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

2017-08-05 Thread Sean McAfee
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)


Re: another one liner

2017-08-04 Thread ToddAndMargo

On 08/04/2017 08:43 PM, Bruce Gray wrote:



On Aug 4, 2017, at 9:12 PM, ToddAndMargo  wrote:

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

2017-08-04 Thread ToddAndMargo

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


another one liner

2017-08-04 Thread ToddAndMargo

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/://'


enp6s0


Many thanks,
-T



# ifconfig
br0: flags=4099  mtu 1500
inet 192.168.255.10  netmask 255.255.255.0  broadcast 
192.168.255.255

inet6 fe80::1886:e8ff:fefb:50c  prefixlen 64  scopeid 0x20
ether 00:00:00:00:00:00  txqueuelen 1000  (Ethernet)
RX packets 161335  bytes 10919435 (10.4 MiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 200479  bytes 144493201 (137.7 MiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp6s0: flags=4099  mtu 1500
ether 00:25:90:20:3b:2a  txqueuelen 1000  (Ethernet)
RX packets 0  bytes 0 (0.0 B)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 0  bytes 0 (0.0 B)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
device interrupt 16  memory 0xfbbe-fbc0

enp7s0: flags=4163  mtu 1500
inet 192.168.250.135  netmask 255.255.255.0  broadcast 
192.168.250.255

inet6 fe80::225:90ff:fe20:3b2b  prefixlen 64  scopeid 0x20
ether 00:25:90:20:3b:2b  txqueuelen 1000  (Ethernet)
RX packets 334640  bytes 239842580 (228.7 MiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 234430  bytes 22392804 (21.3 MiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
device interrupt 17  memory 0xfbce-fbd0

lo: flags=73  mtu 65536
inet 127.0.0.1  netmask 255.0.0.0
inet6 ::1  prefixlen 128  scopeid 0x10
loop  txqueuelen 1  (Local Loopback)
RX packets 362568  bytes 736235075 (702.1 MiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 362568  bytes 736235075 (702.1 MiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099  mtu 1500
inet 192.168.122.1  netmask 255.255.255.0  broadcast 
192.168.122.255

ether 52:54:00:6e:23:13  txqueuelen 1000  (Ethernet)
RX packets 0  bytes 0 (0.0 B)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 0  bytes 0 (0.0 B)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0