Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the range operator '..' to create a utf-8 alphabet?

Tags:

perl

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?

like image 557
dynax60 Avatar asked Mar 05 '11 12:03

dynax60


1 Answers

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.

like image 91
martin clayton Avatar answered Nov 01 '22 09:11

martin clayton