On 3/15/19 6:38 PM, ToddAndMargo via perl6-users wrote:
On 3/14/19 10:05 PM, Todd Chester via perl6-users wrote:
Hi All,
What am I doing wrong here?
ps ax | grep [f]irefox | perl6 -ne 'my @x = $_.words[0].lines.reverse;
print @x[0] ~ "\n";'
7380
7581
7698
13023
13767
13908
Two problems:
1) "lines" is putting everything into @x[0]
2) "reverse" is ignoring me as there is no @x[1]. etc.
The result I want is 13908
Many thanks,
-T
Follow up:
Timo over on the chat line helped with this
sweet way of doing it:
$ pgrep bash | perl6 -e 'my @x = lines.sort.reverse; print @x[0] ~ "\n"
~ @x[1] ~ "\n";'
9873
2659
I did not realize the "lines" was a "slurp" to an array!
:-)
Okay, a little revision. You are going to get the finger
wagged at you if $_ is empty. So we need to add an "if" ...
Empty:
$ pgrep bashx | perl6 -e 'if {my @x = lines.sort.reverse; print @x[0] ~
"\n" ~ @x[1] ~ "\n";}'
===SORRY!===
Expression needs parens to avoid gobbling block
at -e:1
------> se; print @x[0] ~ "\n" ~ @x[1] ~ "\n";}⏏<EOL>
Missing block (apparently claimed by expression)
at -e:1
------> se; print @x[0] ~ "\n" ~ @x[1] ~ "\n";}⏏<EOL>
Empty with "if":
$ pgrep bashx | perl6 -e 'if my @x = lines.sort.reverse {print @x[0] ~
"\n" ~ @x[1] ~ "\n";}'
Not empty with "if":
$ pgrep bash | perl6 -e 'if my @x = lines.sort.reverse {print @x[0] ~
"\n" ~ @x[1] ~ "\n";}'
4780
2354