X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq4.pod;h=18d709169b3fdfe90ca9271405df93d609f798b5;hb=14aaa8fc27b8350048cdee657c0128eb979d0b2a;hp=b5305164318b2745c1c2037523445e7f6d3cdaf8;hpb=881bdbd4cff2623b5a5979fcc7b7c3078938e0fd;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq4.pod b/pod/perlfaq4.pod index b530516..18d7091 100644 --- a/pod/perlfaq4.pod +++ b/pod/perlfaq4.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq4 - Data Manipulation ($Revision: 1.19 $, $Date: 2002/03/11 22:15:19 $) +perlfaq4 - Data Manipulation ($Revision: 1.25 $, $Date: 2002/05/30 07:04:25 $) =head1 DESCRIPTION @@ -135,7 +135,9 @@ functions is that it works with numbers of ANY size, that it is optimized for speed on some operations, and for at least some programmers the notation might be familiar. -=item B +=over 4 + +=item How do I convert hexadecimal into decimal Using perl's built in conversion of 0x notation: @@ -158,7 +160,7 @@ Using the CPAN module Bit::Vector: $vec = Bit::Vector->new_Hex(32, "DEADBEEF"); $dec = $vec->to_Dec(); -=item B +=item How do I convert from decimal to hexadecimal Using sprint: @@ -181,7 +183,7 @@ And Bit::Vector supports odd bit counts: $vec->Resize(32); # suppress leading 0 if unwanted $hex = $vec->to_Hex(); -=item B +=item How do I convert from octal to decimal Using Perl's built in conversion of numbers with leading zeros: @@ -200,7 +202,7 @@ Using Bit::Vector: $vec->Chunk_List_Store(3, split(//, reverse "33653337357")); $dec = $vec->to_Dec(); -=item B +=item How do I convert from decimal to octal Using sprintf: @@ -212,7 +214,12 @@ Using Bit::Vector $vec = Bit::Vector->new_Dec(32, -559038737); $oct = reverse join('', $vec->Chunk_List_Read(3)); -=item B +=item How do I convert from binary to decimal + +Perl 5.6 lets you write binary numbers directly with +the 0b notation: + + $number = 0b10110110; Using pack and ord @@ -231,7 +238,7 @@ Using Bit::Vector: $vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111"); $dec = $vec->to_Dec(); -=item B +=item How do I convert from decimal to binary Using unpack; @@ -246,6 +253,7 @@ Using Bit::Vector: The remaining transformations (e.g. hex -> oct, bin -> hex, etc.) are left as an exercise to the inclined reader. +=back =head2 Why doesn't & work the way I want it to? @@ -1335,28 +1343,21 @@ lists, or you could just do something like this with an array: If you either have Perl 5.8.0 or later installed, or if you have Scalar-List-Utils 1.03 or later installed, you can say: - use List::Util 'shuffle'; + use List::Util 'shuffle'; @shuffled = shuffle(@list); -If not, you can use this: +If not, you can use a Fisher-Yates shuffle. - # fisher_yates_shuffle - # generate a random permutation of an array in place - # As in shuffling a deck of cards - # sub fisher_yates_shuffle { my $deck = shift; # $deck is a reference to an array my $i = @$deck; - while (--$i) { + while ($i--) { my $j = int rand ($i+1); @$deck[$i,$j] = @$deck[$j,$i]; } } -And here is an example of using it: - - # # shuffle my mpeg collection # my @mpeg =