テキストファイル中の Latin-1 未定義コードの置換
Table of Contents
概要
- 置換コードリストに従って、入力テキストファイル中の未定義コードを置換してファイルへ出力する。
 - コード毎に何件置換したかをレポートする。
 
ダウンロード
ソースコード
# テキストファイル中の Latin-1 未定義コードの置き換え
use strict;
use warnings;
use utf8;
my $infile  = "readme.txt";
my $outfile = "readme_.txt";
my %cnvtbl = (
    0x91 => '{{',
    0x92 => '}}',
    0x93 => '[[',
    0x94 => ']]',
);
open( IN, $infile ) || die( "can't open '$infile'.\n" );
my @file = <IN>;
close( IN );
my $body = join( "", @file );
my $len = length( $body );
my ( $i, %codes, $c );
my $outbody = "";
for( $i=0; $i<$len; $i++ ){
    $c = substr( $body, $i, 1 );
    if ( ord( $c ) > 127 ){
        $codes{ ord( $c ) } += 1;
        $c = $cnvtbl{ ord( $c ) };
    }
    $outbody .= $c;
}
foreach $c ( sort( keys( %codes ) ) ){
    printf( "%02x: %d\n", $c, $codes{ $c } );
}
open( OUT, '>'.$outfile ) || die( "can't open '$outfile'.\n" );
print OUT $outbody;
close( OUT );
# EOF