=head1 NAME
-perlfaq4 - Data Manipulation ($Revision: 1.24 $, $Date: 2002/05/20 16:50:08 $)
+perlfaq4 - Data Manipulation ($Revision: 1.25 $, $Date: 2002/05/30 07:04:25 $)
=head1 DESCRIPTION
optimized for speed on some operations, and for at least some
programmers the notation might be familiar.
-=item B<How do I convert hexadecimal into decimal:>
+=over 4
+
+=item How do I convert hexadecimal into decimal
Using perl's built in conversion of 0x notation:
$vec = Bit::Vector->new_Hex(32, "DEADBEEF");
$dec = $vec->to_Dec();
-=item B<How do I convert from decimal to hexadecimal:>
+=item How do I convert from decimal to hexadecimal
Using sprint:
$vec->Resize(32); # suppress leading 0 if unwanted
$hex = $vec->to_Hex();
-=item B<How do I convert from octal to decimal:>
+=item How do I convert from octal to decimal
Using Perl's built in conversion of numbers with leading zeros:
$vec->Chunk_List_Store(3, split(//, reverse "33653337357"));
$dec = $vec->to_Dec();
-=item B<How do I convert from decimal to octal:>
+=item How do I convert from decimal to octal
Using sprintf:
$vec = Bit::Vector->new_Dec(32, -559038737);
$oct = reverse join('', $vec->Chunk_List_Read(3));
-=item B<How do I convert from binary to decimal:>
+=item How do I convert from binary to decimal
Perl 5.6 lets you write binary numbers directly with
the 0b notation:
$vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111");
$dec = $vec->to_Dec();
-=item B<How do I convert from decimal to binary:>
+=item How do I convert from decimal to binary
Using unpack;
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?
characters, a pattern like C</x([^x]*)x/> will get the intervening
bits in $1. For multiple ones, then something more like
C</alpha(.*?)omega/> would be needed. But none of these deals with
-nested patterns, nor can they. For that you'll have to write a
-parser.
+nested patterns. For balanced expressions using C<(>, C<{>, C<[>
+or C<< < >> as delimiters, use the CPAN module Regexp::Common, or see
+L<perlre/(??{ code })>. For other cases, you'll have to write a parser.
If you are serious about writing a parser, there are a number of
modules or oddities that will make your life a lot easier. There are
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>):
You can also use the L<Data::Types|Data::Types> module on
the CPAN, which exports functions that validate data types
-using these and other regular expressions.
+using these and other regular expressions, or you can use
+the C<Regexp::Common> module from CPAN which has regular
+expressions to match various types of numbers.
If you're on a POSIX system, Perl's supports the C<POSIX::strtod>
function. Its semantics are somewhat cumbersome, so here's a C<getnum>