Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Shorten this one-liner!

by LanX (Saint)
on Aug 25, 2018 at 21:56 UTC ( [id://1221106]=note: print w/replies, xml ) Need Help??


in reply to Shorten this one-liner!

Update: Code shown is based on the last one-liner of the OP which was case sensitive.

For case insensitivity replace /a/ with /a/i and y/e/.. with y/eE/.. in the following examples.


For a start, I tried this

c:\Perl_524>perl -anE"/a/&&say$_,' :'.y/$_/e/for@F" anyone cnacel declare perlmonks anyone :0 cnacel :0 declare :0
but am failing to call tr (aka y) in scalar context.

update
I misread the docs for tr, is this good enough?
c:\Perl_524>perl -anE"/a/&&say$_.' :'.y/e/e/for@F" anyone cancel declare perlmonks anyone :1 cancel :1 declare :2

note: you have to enter more lines or kill the one liner with C-c

update

one char less!

c:\Perl_524>perl -anE"/a/&&say$_.' :'.y/e//for@F" anyone cancel declare perlmonks anyone :1 cancel :1 declare :2

explanation: this is equivalent

use feature 'say'; while (<>) { @F = split(' '); /a/ and say $_ . ' :' . tr/e// foreach (@F); }

see also perlrun for -a, -n and -E

(though it claims "-a implicitly sets -n" which I can't reproduce)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

update

even less under linux

~$ perl -anE'/a/&&say"$_ :".y/e//for@F' anyone cancel declare perlmonks anyone :1 cancel :1 declare :2

update
last but not least without reading from STDIN (which doesn't make much sense for a one-liner)

$ perl -E'/a/&&say"$_ :".y/e//for qw/anyone cancel declare perlmonks/' anyone :1 cancel :1 declare :2

Replies are listed 'Best First'.
Re^2: Shorten this one-liner!
by lunix (Novice) on Aug 26, 2018 at 10:03 UTC
    Thank you for taking the time and replying! I tried using say, but I have never done it before, so I didn't succeed that way. And I knew the substitution (s) operator returned by default the amount of substitutions made, but I couldn't use it on a read-only value, so tr was the asnwer, but I didn't know it existed! Always good to learn something new.
    Your final code was

    perl -E'/a/&&say"$_ :".y/e//for qw/anyone cancel declare perlmonks/'

    Which is great, and already shorter than mine, but I was able to get it even shorter!

    perl -E'/a/&&say"$_ :".y/e//for anyone,cancel,declare,perlmonks'

    And reading from STDIN is a good idea, (and shortens the code even furter), but I wanted to get a one-liner that can just be copy-pasted in itself and does the job, without having to type anything.

    Cheers, lunix

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1221106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found