| Newsgroups |
perl.cvs.perlfaq |
| Message-ID |
<[email protected]> |
cvsuser 02/05/16 05:44:24
Modified: . perlfaq4.pod
Log:
==> How do I process/modify each element of an array?
* updated comment about values() making a copy within the
foreach context (new as of 5.6.x)
==> How do I manipulate arrays of bits?
* added another example from Benjamin Goldberg
Revision Changes Path
1.22 +19 -5 perlfaq/perlfaq4.pod
Index: perlfaq4.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -w -r1.21 -r1.22
--- perlfaq4.pod 6 May 2002 13:08:46 -0000 1.21
+++ perlfaq4.pod 16 May 2002 12:44:24 -0000 1.22
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq4 - Data Manipulation ($Revision: 1.21 $, $Date: 2002/05/06 13:08:46 $)
+perlfaq4 - Data Manipulation ($Revision: 1.22 $, $Date: 2002/05/16 12:44:24 $)
=head1 DESCRIPTION
@@ -1396,13 +1396,20 @@
$_ *= (4/3) * 3.14159; # this will be constant folded
}
-If you want to do the same thing to modify the values of the hash,
-you may not use the C<values> function, oddly enough. You need a slice:
+If you want to do the same thing to modify the values of the
+hash, you can use the C<values> function. As of Perl 5.6
+the values are not copied, so if you modify $orbit (in this
+case), you modify the value.
- for $orbit ( @orbits{keys %orbits} ) {
+ for $orbit ( values %orbits ) {
($orbit **= 3) *= (4/3) * 3.14159;
}
+Prior to perl 5.6 C<values> returned copies of the values,
+so older perl code often contains constructions such as
+C<@orbits{keys %orbits}> instead of C<values %orbits> where
+the hash is to be modified.
+
=head2 How do I select a random element from an array?
Use the rand() function (see L<perlfunc/rand>):
@@ -1539,6 +1546,13 @@
This method gets faster the more sparse the bit vector is.
(Courtesy of Tim Bunce and Winfried Koenig.)
+
+You can make the while loop a lot shorter with this suggestion
+from Benjamin Goldberg:
+
+ while($vec =~ /[^\0]+/g ) {
+ push @ints, grep vec($vec, $_, 1), $-[0] * 8 .. $+[0] * 8;
+ }
Or use the CPAN module Bit::Vector: