从不同文件中读取而不保留旧内容perl

时间:2016-07-19 13:02:50

标签: perl stdin filereader fileinputstream filehandle

所以我有一个函数可以打开要读取的文件,分析行并在新文件中写入一些。我用不同的文件多次调用这个funktion。但是现在我注意到,通过每次新的函数调用,前面文件的行也会被读入。我怎么能防止这种情况?

es.txt的内容: El anarquismo es una filosofía política y social que llama

dt.txt的内容: der Regel mit Veränderungen der chemischen Bindungen in

程序运行后,创建的文件ProfileDE看起来像这样(虽然它应该只包含来自" dt.txt"的标记,有些标记来自" es.txt"):

una
ilos
ism
lític
qui
lí
ti
polí
socia

- 实际代码:

#! /usr/bin/perl
use utf8;
use warnings;
use strict;
use List::Util qw(min);
use open ':encoding(utf8)';
binmode(STDOUT, ":utf8");
binmode(STDIN, ":utf8");

generateProfile("es.txt", "ES"); #function call to read from file es.txt 
generateProfile("dt.txt", "DE"); #second call to read only from file dt.txt

sub generateProfile { 
    my $file= $_[0]; #taking arguments
    my $Lang = $_[1];

    open(IN, "<:utf8",$file) || die "error"; #to read file 
    open(OUT, ">:utf8", "profile$Lang.txt"); # to create and write in file e.g profileDe

    my (%ngramL); #any hash for later
    my $line; 
    my (@words);
    my (%ngramL);
    my (@uni, @bi, @tri, @quad, @five); #array which keeps letterkombinations of different length

    while($line =<IN>){ 
        chomp $line;
       # print $line;  # just for testing: during the second function call, it would print here old content from "es.txt" instead of only reading from "dt.txt"
        push(@words, $line);
        }
     close IN;   #doesn't it closed?

     foreach my $word (@words){
        bigramm($word); #split word in different letter combinations
        }

    freqL(); #fill that hash with frequences, how many times occures one letter combination e.g. "ab" = 2, "tion"=5
    print_hashL(); #print hash 

    sub bigramm{
      my $wort= $_[0];
      my $i; my $k;
      my @letters= split(//, $wort);
      for ($i=0; $i<length($wort)-0; $i++){ ####!!!!! -1?
        my $bi= substr($wort, $i, 1);
        push(@uni, $bi); }   
      for ($i=0; $i<length($wort)-1; $i++){
        my $bi= substr($wort, $i, 2);
        push(@bi, $bi); }
      for ($i=0; $i<length($wort)-2; $i++){
        my $bi= substr($wort, $i, 3);
        push(@tri, $bi); }
      for ($i=0; $i<length($wort)-3; $i++){
        my $bi= substr($wort, $i, 4);
        push(@quad, $bi); }
      for ($i=0; $i<length($wort)-4; $i++){
        my $bi= substr($wort, $i, 5);
        push(@five, $bi); }      

 }

    sub freqL{
      for my $duo (@uni, @bi, @tri, @quad, @five){
        if(defined $ngramL{$duo}) {$ngramL{$duo}++;}
        else {$ngramL{$duo}=1;}
    }
  }

    sub print_hashL{
      foreach my $elem(sort{$ngramL{$b}<=>$ngramL{$a}} keys %ngramL) {
        print OUT "$elem\n";}
     }

}

还有一些警告,可能会也可能不会导致这个问题? :

"my" variable %ngramL masks earlier declaration in same scope at stack.pl line 23.
Variable "@uni" will not stay shared at stack.pl line 46.
Variable "@bi" will not stay shared at stack.pl line 49.
Variable "@tri" will not stay shared at stack.pl line 52.
Variable "@quad" will not stay shared at stack.pl line 55.
Variable "@five" will not stay shared at stack.pl line 58.
Variable "@uni" will not stay shared at stack.pl line 63.
Variable "@bi" will not stay shared at stack.pl line 63.
Variable "@tri" will not stay shared at stack.pl line 63.
Variable "@quad" will not stay shared at stack.pl line 63.
Variable "@five" will not stay shared at stack.pl line 63.
Variable "%ngramL" will not stay shared at stack.pl line 64.
Variable "%ngramL" will not stay shared at stack.pl line 70.

1 个答案:

答案 0 :(得分:0)

while($line =<IN>){ 
    chomp $line;
    print $line;  # during the second function call, it would print here old content from "es.txt" instead of only reading from "dt.txt"
    push(@words, $line);
    close IN;   #doesn't it closed?

从输入文件中读取一行后,基本上关闭了文件处理程序。然后,当你转到第二行时,你因为之前关闭它而无法从文件中读取。