perlquestion
Anonymous Monk
<p>
Oh monks most exalted and wise, a humble novice seeks to benefit from your vast knowledge of all things Perl.
</p><p>
I have a text file (UTF8-encoded) containing words, one per line, and I'm interested in finding out, for each word, how many characters are in it that do not appear in the English alphabet. So I whipped up a script to find out:
</p>
<code>
#!/usr/bin/perl
use open IO => ':utf8';
while(<>) {
chomp;
($nonenglish = $_) =~ s/[A-Za-z]//g;
print length($nonenglish), " $nonenglish\n";
}
</code>
<p>
Alas, it isn't working; length appears to be counting bytes rather than characters! To wit:
</p>
<code>
$ perl nonenglish.pl
æ
2 æ
ꝥ
3 ꝥ
𪛖
4 𪛖
^D
$
</code>
<p>
I only occasionally use Perl, and I have no idea what I'm doing wrong. Enlighten me, kind monks, I beseech you!
</p><p>
My Perl is 5.14.2, BTW (and I cannot easily upgrade).
</p>
25