Perl Christmas Quiz 2009
Mark Zealey
mspam at itsolve.co.uk
Tue Dec 1 13:13:15 GMT 2009
> 7) Write a one line program that takes a non-negative integer as an
> argument and prints the square root when the answer's an integer.
>
>
>
> Restrictions: the perl line should be a regular expression.
> You are allowed to use the following functions/operators "x", "-",
> "length", "print" plus any of the usual regular expression bestiary.
I'm pretty close but can't find a proper way to go to the previous capture
group. eg the following works as much as you care to extend it:
("a" x $ARGV[0]) =~ /^
(a)
( (a\g{-3})\g{-3} )?
( (a\g{-3})\g{-3} )?
( (a\g{-3})\g{-3} )?
( (a\g{-3})\g{-3} )?
( (a\g{-3})\g{-3} )?
( (a\g{-3})\g{-3} )?
$
(?{print length $+})
/x;
However the obvious solution of using ( (a\g{-3})\g{-3} )* doesnt work as the
capture buffers arn't updated on each match; and I can't manage to get the
newer recursive syntax of (?-X) working properly. This would be worse anyway
because then you could only go up to 50*50 before you get killed for deep
recursion. The must be a way perhaps using the (?| syntax to get the capture
buffers updated each time round?
Any thoughts?
Mark
More information about the london.pm
mailing list