Huh - so apparently your method and my evil method ended up being the same, or at least very similar. Note that you can improve this slightly by consolidating your last two expressions and eliminating the need for an "e" flag on the substitutions:
for ($str) {
s/0(0*)$/1$1/
or s/1(0*)1$/01$1/
or return;
}
And actually, we can combine your method and the last bit in my post to get this somewhat natural structure for looping through the possibilities:
sub nextmask {
if ($_[0] =~ s/0(0*)$/1$1/) {1;}
elsif ($_[0] =~ s/1(0*)1$/01$1/) {2;}
else {0;}
}
my @factors = qw(2 3 5 7);
my $mask = '0' x scalar(@factors);
while (my $transtype = nextmask($mask))
{
print "BREAK\n" if ($transtype > 1);
# do stuff
print join " ",
map { substr($mask, $_, 1) ? $factors[$_] : ' ' }
0 .. $#factors;
print "\n";
}
(I've never liked do { ... } while() loops)
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/;
map{y/X_/\n /;print}map{pop@$_}@/for@/
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|