The node The Oldest Plays the Piano inspired me a bit playing with regular expressions on numbers represented by '1'x$nr. Something works fine (see below: checking for "dividable by 5" or the famous prime-check). Unfortunately I don't manage to check if the number is a square - at least not with pure regexes (see my tries below) - and my google-Fu is not good enough to find a hint online.
Can you please enlighten me?my $nr = 25; my $n = 5-1; print "dividable by 5 :-)\n" if (1 x $nr) =~ /^(1+)\1{$n}$/; + # works print "prime\n" if (1 x $nr) !~ /^(11+)\1+$/; + # works (1 x $nr) !~ /^(1+)(1*)$ (?{ sq($1)}) (0) /x; + # works - but not what I want ... sub sq { print "SQUARE\n" if ((length($_[0]) * length( +$_[0])) == $nr) ; } my $len = 'length($1)-1'; print "square2 :-)\n" if (1 x $nr) =~ /^(1+)\1{$len}$/; + # this won't work print "square3 :-)\n" if (1 x $nr) =~ /^(1+)\1{length($1)-1}$/ +; # this won't work .. but what will?!?!?!
In reply to check for square-number with a regex by Ratazong
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |