first commit
This commit is contained in:
35
perl/Examples/Chap4/make-genes-1
Normal file
35
perl/Examples/Chap4/make-genes-1
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
|
||||
###
|
||||
### make_genes
|
||||
###
|
||||
|
||||
## Chapter 4 section 3.2
|
||||
|
||||
sub make_genes {
|
||||
my $pat = shift;
|
||||
my @tokens = split /[()]/, $pat;
|
||||
for (my $i = 1; $i < @tokens; $i += 2) {
|
||||
$tokens[$i] = [0, split(//, $tokens[$i])];
|
||||
}
|
||||
my $FINISHED = 0;
|
||||
return Iterator {
|
||||
return if $FINISHED;
|
||||
my $finished_incrementing = 0;
|
||||
my $result = "";
|
||||
for my $token (@tokens) {
|
||||
if (ref $token eq "") { # plain string
|
||||
$result .= $token;
|
||||
} else { # wildcard
|
||||
my ($n, @c) = @$token;
|
||||
$result .= $c[$n];
|
||||
unless ($finished_incrementing) {
|
||||
if ($n == $#c) { $token->[0] = 0 }
|
||||
else { $token->[0]++; $finished_incrementing = 1 }
|
||||
}
|
||||
}
|
||||
}
|
||||
$FINISHED = 1 unless $finished_incrementing;
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user