2014-01-10 4 views
0

문자열로 파일을 읽으려고 시도하고 각 문자의 유니 코드 범위 2816-2943을 확인하려고합니다. 범위 및 \ n에 해당하는 문자를 제외하고는 다른 모든 문자를 건너 뛸 필요가 있습니다. 나는 그물에서 다음 코드를 가지고 있지만 나를 위해 일하지 않습니다. 나는 어리석은 실수를한다면 미안하다. 나는 펄을 처음 사용한다. plz 오늘 내가 끝내야 할 도움이.perl이 파일을 문자열로 읽어 들인 다음 각 문자의 유니 코드 범위를 확인하십시오.

use utf8; 
use encoding 'utf8'; 
use open qw/:std :utf8/; 

binmode(STDOUT, ":utf8"); #makes STDOUT output in UTF-8 instead of ordinary ASCII. 


$file="content.txt"; 
open FILE1, ">filtered.txt" or die $!; 
    open(FILE, "<$file") or die "Can't read file 'filename' [$!]\n"; 
    binmode(FILE); 
    my $document = <FILE>; 
    close (FILE); 
    print $document; 
+1

코드가 작동하지 않는 이유는 단순히 한 파일의 내용을 다른 파일로 복사하기 때문에 필터링이 필요하지 않기 때문입니다. – ThisSuitIsBlackNot

답변

1

다음은 $input 파일에서 라인으로 라인을 읽고 $output 파일에 필터링 된 행을 기록합니다.

my $input = 'content.txt'; 
my $output = 'filtered.txt'; 

open(my $src_fh, '<:encoding(UTF-8)', $input) 
    or die qq/Could not open file '$input' for reading: '$!'/; 

open(my $dst_fh, '>:encoding(UTF-8)', $output) 
    or die qq/Could not open file '$output' for writing: '$!'/; 

while(<$src_fh>) { 
    s/[^\x{0B00}-\x{0B7F}\n]//g; 
    print {$dst_fh} $_ 
     or die qq/Could not write to file '$output': '$!'/; 
} 

close $dst_fh 
    or die qq/Could not close output filehandle: '$!'/; 

close $src_fh 
    or die qq/Could not close input filehandle: '$!'/;