文字数 と バイト数

文字数 と バイト数

lesson011.pl


print "文字数\n";
foreach $arg ( @ARGV )
{
print $arg, " => ", charCount($arg), "\n";
}

print "バイト数\n";
foreach $arg ( @ARGV )
{
print $arg, " => ", length($arg), "\n";
}

sub charCount
{
($_) = @_;

# 全角文字数を数え、全角文字を消去する
$lz = s/[\x81-\x9f\xe0-\xfc][\x40-\x7e\x80-\xfc]//g;

# 半角文字数を数える
$lh = length($_);

# 全角・半角の文字数を足して返す
return $lz + $lh;
}

実行結果

C:\>perl c:\study\perl\chapter001\lesson011.pl 12 12 12
文字数
12 => 2
12 => 2
12 => 2
バイト数
12 => 2
12 => 4
12 => 3