Processing a String One Character at a Time
Problem
You want to process a string one character at a time.
Solution
Use split with a null pattern to break up the string into individual characters, or use unpack if you just want their ASCII values:
@array = split(//, $string);
@array = unpack("C*", $string);Or extract each character in turn with a loop:
while (/(.)/g) { # . is never a newline here
# do something with $1
}Discussion
As we said before, Perl’s fundamental unit is the string, not the character. Needing to process anything a character at a time is rare. Usually some kind of higher-level Perl operation, like pattern matching, solves the problem more easily. See, for example, Section 7.7, where a set of substitutions is used to find command-line arguments.
Splitting on a pattern that matches the empty string returns a list
of the individual characters in the string. This is a convenient
feature when done intentionally, but it’s easy to do
unintentionally. For instance, /X*/ matches the
empty string. Odds are you will find others when you don’t mean
to.
Here’s an example that prints the characters used in the string
"an
apple
a
day“, sorted in ascending ASCII order:
%seen = ();
$string = "an apple a day";
foreach $byte (split //, $string) {
$seen{$byte}++;
}
print "unique chars are: ", sort(keys %seen), "\n";
unique chars are: adelnpyThese split and unpack solutions give you an array of characters to work with. If you don’t want an array, you can use a pattern match with the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access