I did not find your code particularly easy to understand.
An alternate coding could be like the following...
tr is a lot faster than using the regex engine.
#!/usr/bin/perl use warnings; use strict; my @words = ("anyone", "cancel", "declArE", "perlmonks", "pal"); # Note the "pal" case is an extra case that I added foreach my $word (@words) { if ($word =~ tr/aA//) # word contains an "A" or "a" { my $numE = $word =~ tr/eE//; print "$word $numE\n"; #number of e's in word with an "a" } } __END__ anyone 1 cancel 1 declArE 2 pal 0
I think shortening the code to a one-liner is a meaningless exercise.

Update: I don't see the reason to make a "one liner" other than to do it for the fun of doing it. This makes no difference in execution speed. White space and comments consume no MIPS and the difference in Perl compile speed is insignificant. This $words[$count] is very anti-Perl philosophy... subscripts are rare. Learn more Perl before trying to write a one-liner.

Another Update:
This will be about the same performance as the above code:

my @words = ("anyone", "cancel", "declArE", "perlmonks", "pal"); print "$_: ",tr/eE//, "\n" for grep{tr/aA//;}@words;
I asked a Python friend and here is one result:
words = ["anyone", "cancel", "declArE", "perlmonks", "pal"] print(*((word,word.upper().count('E')) for word in words if 'A' in wor +d.upper()))
I like my Perl code much better!

In reply to Re: Shorten this one-liner! by Marshall
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.