Is there a way to create a UTF-8 alphabet array using the Perl '..' operator?
For example, this one won't work:
$ cat t.pl
#!/usr/bin/perl
use Data::Dumper;
use encoding 'utf8';
print Dumper('А'..'Я'); # not working!
print Dumper('А','Б','В'); # ...works fine! but needs to be filling letter by letter
$ perl t.pl
$VAR1 = "\x{410}";
$VAR1 = "\x{410}";
$VAR2 = "\x{411}";
$VAR3 = "\x{412}";
$ echo $LANG
en_US.UTF-8
Any advice?
This is mentioned - briefly - in the range operator docs. You need to use the ord and chr functions:
#!/usr/bin/perl
use Data::Dumper;
use encoding 'utf8';
my @arry = map { chr } ord( 'А' ) .. ord( 'Я' );
for my $letter ( @arry ) {
print "$letter ";
}
print "\n";
Output:
А Б В Г Д Е Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Ъ Ы Ь Э Ю Я
The result you see arises because the initial value of the range isn't part of a 'magical' sequence (a non-empty string matching /^[a-zA-Z]*[0-9]*\z/
), so the operator just returns that initial value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With