On 01/08/2011 13:14, Emeka wrote:
I would like to know how to access character from string lateral. Say I have $foo = "From Big Brother Africa"; I would want to print each of the characters of $foo on its own. In some languages string type is just array/list of characters. What is it in Perl?
Hi Emeka Perl is rich with string-handling operators. Take a look at perldoc perlfunc and look at "Perl Functions by Category". Part of it reads
Functions for SCALARs or strings "chomp", "chop", "chr", "crypt", "hex", "index", "lc", "lcfirst", "length", "oct", "ord", "pack", "q//", "qq//", "reverse", "rindex", "sprintf", "substr", "tr///", "uc", "ucfirst", "y///" Regular expressions and pattern matching "m//", "pos", "quotemeta", "s///", "split", "study", "qr//"
So what you would ordinarily do in C by indexing an array of characters can have many better solutions in Perl. As has been mentioned by others, my @chars = split //, $string; will give you an array of one-character strings that you may be able to handle as if you were using a different language, but that is rarely the way to go if you are writing a Perl program. Perl regular expressions are very felxible and comprehensive, and you will find that most string operations are best expressed that way rather than using split, index, substr and so on. If you describe your goal then we would be able to help you better. Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/