cvs commit: perlfaq perlfaq4.pod
[email protected] (brian d foy)
| Newsgroups | perl.cvs.perlfaq |
|---|---|
| Message-ID | <[email protected]> |
cvsuser 03/01/03 12:06:21
Modified: . perlfaq4.pod
Log:
* 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)
Revision Changes Path
1.39 +10 -2 perlfaq/perlfaq4.pod
Index: perlfaq4.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -w -r1.38 -r1.39
--- perlfaq4.pod 6 Dec 2002 07:40:11 -0000 1.38
+++ perlfaq4.pod 3 Jan 2003 20:06:21 -0000 1.39
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq4 - Data Manipulation ($Revision: 1.38 $, $Date: 2002/12/06 07:40:11 $)
+perlfaq4 - Data Manipulation ($Revision: 1.39 $, $Date: 2003/01/03 20:06:21 $)
=head1 DESCRIPTION
@@ -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