=head1 NAME
-perlfaq4 - Data Manipulation ($Revision: 1.15 $)
+perlfaq4 - Data Manipulation ($Revision: 1.17 $, $Date: 1997/03/25 18:16:24 $)
=head1 DESCRIPTION
You have to keep track. For example, let's say you want
to change the fifth occurrence of "whoever" or "whomever"
-into "whosoever", case insensitively.
+into "whosoever" or "whomsoever", case insensitively.
$count = 0;
s{((whom?)ever)}{
$string = "ThisXlineXhasXsomeXx'sXinXit":
$count = ($string =~ tr/X//);
- print "There are $count X charcters in the string";
+ print "There are $count X characters in the string";
This is fine if you are just looking for a single character. However,
if you are trying to count multiple character substrings within a
=head2 How do I capitalize all the words on one line?
To make the first letter of each word upper case:
+
$line =~ s/\b(\w)/\U$1/g;
To make the whole line upper case:
+
$line = uc($line);
To force each word to be lower case, with the first letter upper case:
+
$line =~ s/(\w+)/\u\L$1/g;
=head2 How can I split a [character] delimited string except when inside
If you mean how many keys, then all you have to do is
take the scalar sense of the keys() function:
- $num_keys = scalar keys %hash;
+ $num_keys = scalar keys %hash;
In void context it just resets the iterator, which is faster
for tied hashes.
Using C<keys %hash> in a scalar context returns the number of keys in
the hash I<and> resets the iterator associated with the hash. You may
need to do this if you use C<last> to exit a loop early so that when you
-re-enter it, the hash iterator has been reset.
+reenter it, the hash iterator has been reset.
=head2 How can I get the unique keys from two hashes?
I<not> cause that key to be forever there. This is different than
awk's behavior.
-=head2 How can I make the Perl equivalent of a C structure/C++ class/hash
+=head2 How can I make the Perl equivalent of a C structure/C++ class/hash
or array of hashes or arrays?
Use references (documented in L<perlref>). Examples of complex data
If you're concerned about 8-bit ASCII data, then see L<perllocale>.
-If you want to deal with multi-byte characters, however, there are
+If you want to deal with multibyte characters, however, there are
some gotchas. See the section on Regular Expressions.
=head2 How do I determine whether a scalar is a number/whole/integer/float?
warn "has nondigits" if /\D/;
warn "not a whole number" unless /^\d+$/;
warn "not an integer" unless /^-?\d+$/; # reject +3
- warn "not an integer" unless /^[+-]?\d+$/;
+ warn "not an integer" unless /^[+-]?\d+$/;
warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2
warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
warn "not a C float"