我是perl的新手。我有一個要求,我必須將字符串中的UTF-8字符轉換爲連字符( - )。Perl - 將utf-8 char轉換爲連字符 - 將utf-8讀爲單個字符
Input string - "IVM IST 20150324095652 31610150096 10ÑatÑ25ÑDisco 0000000091"
Expected output - "IVM IST 20150324095652 31610150096 10-at-25-Disco 0000000091".
但以下方案,其中我已經寫,讀取UTF-8字符作爲兩個獨立的字節和得到的輸出爲 「10 - 在 - 25 - 迪斯科」
[[email protected] cdr]# cat ../asciifilter.pl
#!/usr/bin/perl
use strict;
use Encode;
my @chars;
my $character;
my $num;
while(my $row = <>) {
@chars = split(//,$row);
foreach $character (@chars) {
$num = ord($character);
if($num < 127) {
print $character;
} else {
print "-";
}
}
}
輸出:
[[email protected] cdr]# echo "IVM IST 20150324095652 31610150096 10ÑatÑ25ÑDisco 0000000091" | ../asciifilter.pl
IVM IST 20150324095652 31610150096 10--at--25--Disco 0000000091
但這特定第四字符串列具有14個字符的固定長度only.So附加連字符正在創建的問題。
有人可以給我一些關於如何讀取UTF-8字符作爲單個字符的線索嗎?
'使用開放「:STD」,':編碼(UTF-8) ;' – ikegami 2015-04-01 14:32:18