Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
So, I'm trying (for whatever reason) to split a string into its component characters. This is what I do:
my @letters = split //, @text
But there are some non-ASCII characters (quite a variety, actually) in the text, so in the code, there are the following lines:
use locale; use POSIX qw(locale_h); use POSIX; use Encode; my $old_lcall = setlocale(LC_ALL); my $old_ctype = setlocale(LC_CTYPE); setlocale(LC_ALL,"cs_CZ.utf8"); setlocale(LC_CTYPE,"cs_CZ.utf8"); # ...the $text variable has also been encoded as UTF-8 (the # hyphenated version), using the encode function provided # by use Encode; my @letters = split //, $text; # and at the end of the code, set the locales back: setlocale(LC_ALL, $old_lcall); setlocale(LC_CTYPE, $old_ctype);
Looking at the output, split reads the string in $text byte-by-byte (while sort, for example, works as it should under the locale). So the question is, o wise Perl monks, how do I get split to obey the locale as well?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Split is ignoring locale settings
by Anonymous Monk on Oct 27, 2011 at 17:31 UTC | |
|
Re: Split is ignoring locale settings
by Anonymous Monk on Oct 27, 2011 at 17:16 UTC |