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

Is there a way to create a UTF-8 alphabetical array using the Perl '..' operator?

For example, this one will not 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 tips?

+5
source share
1 answer

This is mentioned - in short - in range range operators . You need to use 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";

Conclusion:

                               

The result that you see is due to the fact that the initial value of the range is not part of the "magic" sequence (non-empty string corresponding to /^[a-zA-Z]*[0-9]*\z/), so the operator simply returns this initial value.

+15

All Articles