Front page | perl.fwp |
Postings from January 2002
Re: even.pl solutions
Thread Previous
|
Thread Next
From:
Philippe 'BooK' Bruhat
Date:
January 28, 2002 16:15
Subject:
Re: even.pl solutions
Message ID:
Pine.LNX.4.21.0201290058430.14889-100000@rose.home.bruhat.net
On Mon, 28 Jan 2002, Stephen Turner wrote:
> Well, I'd just like to thank Andrew for organising this. I've had fun,
> anyway.
>
> Now I hope someone will explain Eugene's solution to us poor newbies!
OK, this might not be very clear, but anyway...
-p $_ x=1&~$.&split$&while aeiouy=~/.?/g
-p prints $_ (but you know this)
On each turn of the loop $_ is "multipled" by
1&~$.&split$&
(yes, this & is not an operator)
with a little more parentheses :
$_ x= (1 & (~$.) & (split $&)) while(aeiouy=~/.?/g)
So we print $_ multiplied by 1 if all of those have 1 as their low-order
bit. And by 0, if any of those numbers is even.
~$. is odd if $. is even
aeiouy=~/./g loops on the 6 vowels.
But what's the use of the question mark, you ask?
You can test with these:
$ perl -e 'print "$&:" while aeiouy=~/./g'
a:e:i:o:u:y:
$ perl -e 'print "$&:" while aeiouy=~/.?/g'
a:e:i:o:u:y::
The difference is that then it also matches the empty string at the end of
"aeiouy"...
split$& splits on $&, that is to say successively a, e, i, o, u, y and ''.
this is odd if the number of a, e, i, o, u, y and total letters is even.
In the end the resulting number is 1 if all the calculated numbers were
odd. If any of those numbers is even, 1&that number returns 0, so $_ is
"multiplied" by 0, and we print nothing.
Well, bravo! I must admit I find it quite "interesting" to try to count an
odd number of things while almost everybody else was looking for even
counts!
Other nice entries were the ones that used s/$&/$&/ while aeiouy=~/.?/g
I love this use of two different values of $& (which are equal, but not
the same)
--
Philippe "BooK" Bruhat
The shortest distance between two points is not always the safest.
(Moral from Groo The Wanderer #69 (Epic))
Thread Previous
|
Thread Next