I think shortening the code to a one-liner is a meaningless exercise.#!/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
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:
I asked a Python friend and here is one result:my @words = ("anyone", "cancel", "declArE", "perlmonks", "pal"); print "$_: ",tr/eE//, "\n" for grep{tr/aA//;}@words;
I like my Perl code much better!words = ["anyone", "cancel", "declArE", "perlmonks", "pal"] print(*((word,word.upper().count('E')) for word in words if 'A' in wor +d.upper()))
In reply to Re: Shorten this one-liner!
by Marshall
in thread Shorten this one-liner!
by lunix
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |