Whats the fasted way to look at a single character?
my $s = "a1b2";
if ( $s[0] eq "a" ) {
In the above example (which is more C than Perl, by the way, but I think
you knew that) you check to see if the first character of $s is "a". You
can do that with a simple regular expression, with no need to "look at"
the first character as a separate variable:
if ($s =~ /^a/) { ... }
Or, if the character is part of a pattern that might appear at any point
within the string, you could use "(.)" within a regex to capture the
character in a subexpression:
my ($first, $last) = ($s =~ /^(.).*(.)$/);
If you want an array with all of the characters as elements, split() the
string with an empty delimiter:
my $chars = split(//, $s);
Of course, you could use substr() to extract a single character from a
string, if you know the index at which it appears within the string:
my $first = substr($s, 0);
And that's without considering the handful of oddball functions for
doing various things to single characters within a string, for example
ucfirst() or lcfirst().
In short, there are many ways to "look at" a single character. Which one
is best is very much dependent on *why* you want to look at it.
sherm--