To clarify that a word containing an 'a' but no 'e' should be printed, I've added an extra "a" word to your test data. Your program:

for('a','anyone','cancel','declare','perlmonks'){$i=()=/e/g;print"$_: +$i\n"if(/a/);}
and mine:
/a/&&print"$_: ".y/e//.$/for'a','anyone','cancel','declare','perlmonks +'
both print the identical output namely:
a: 0 anyone: 1 cancel: 1 declare: 2
but mine is 13 characters shorter.

For golf problems of this type I prefer to put the data not in code, but in a separate file read by the program from stdin. For example, for a data file t1.txt containing:

a anyone cancel declare perlmonks
we can run:
perl -lne"/a/&&print$_.': '.y/e//" t1.txt
to produce the same output.

Update: Just saw LanX -E switch update. For modern versions of perl, we can shorten to:

perl -lnE"/a/&&say$_.': '.y/e//" t1.txt
which should work everywhere. For Unix only, we can save another stroke or two by using -lnE'...' instead of -lnE"...".


In reply to Re: Shorten this one-liner! by eyepopslikeamosquito
in thread Shorten this one-liner! by lunix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.