perlfaq4: multiple answers affected
[email protected] (_brian_d_foy)
| Newsgroups | perl.perlfaq.workers |
|---|---|
| Message-ID | <291220020251421330%[email protected]> |
* How do I find the first array element for which a condition is true?
+ the first assignment should be to the variable which
remembers the found index, which is $index, not $i (the trial
index).
* How do I permute N elements of a list?
+ added a faster running example (from Benjamin Goldberg)
Index: perlfaq4.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
retrieving revision 1.38
diff -u -d -r1.38 perlfaq4.pod
--- perlfaq4.pod 6 Dec 2002 07:40:11 -0000 1.38
+++ perlfaq4.pod 29 Dec 2002 10:44:42 -0000
@@ -1280,7 +1280,7 @@
and check the array element at each index until you find one
that satisfies the condition.
- my( $found, $i ) = ( undef, -1 );
+ my( $found, $index ) = ( undef, -1 );
for( $i = 0; $i < @array; $i++ )
{
if( $array[$i] =~ /Perl/ )
@@ -1455,6 +1455,14 @@
while (my @perm = $p_iterator->next) {
print "next permutation: (@perm)\n";
}
+
+For even faster execution, you could do:
+
+ use Algorithm::Permute;
+ my @array = 'a'..'d';
+ Algorithm::Permute::permute {
+ print "next permutation: (@array)\n";
+ } @array;
Here's a little program that generates all permutations of
all the words on each line of input. The algorithm embodied
--
brian d foy, [email protected]