My immediate problem was checking a specific position in a string for a specific value (if the char in the first position == '#', skip the record). substr works OK in that case. However, I also want to learn more and it seems regex is a better and more powerful answer.
Thank you, Chuck On Tue, 2009-09-01 at 21:55 -0400, Uri Guttman wrote: > >>>>> "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 > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/