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)
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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.