>>>>> "CC" == Chuck Crisler <charles.cris...@comcast.net> writes:

  CC> How do I access specific character positions in a scalar string? In case
  CC> it makes any difference, I specifically want to test the zeroth
  CC> character. Something like the following.

  CC> my $str = "abc";
  CC> if ($str[0] == '#')
  CC> {
  CC>   do something...
  CC> }

you already found substr. but i will guess your real need isn't to scan
a string char by char or to directly index for a char. my suspicious
nose tells me you are parsing a string and substr is the wrong way to do
it. regexes will get you each char or more very easily. or you can split
the string and loop over it that way. but calling substr for each char
is clumsy and slow. if you want to know that the string begins with a
char, this regex will do:

        $str =~ /^a/

so ask your real question about string munging and not how to do a
particular technique. this is called the XY problem where you think you
want X but you really want Y.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to