1

いくつかの行を持つタブ区切りのテキストファイルがあります。行を配列に割り当てるスクリプトを作成し、正規表現を使用して配列を検索し、特定の条件に一致する行を見つけました。一致するものが見つかったら、それをOutput1に書き込みます。リストされているすべてのifステートメント(正規表現)を調べても基準が満たされない場合、その行は出力2に書き込まれます。

一致基準と出力1への書き込みに関しては、100%動作しますが、ここで問題が発生します。一致した行は、一致しない行とともにOutput2にも書き込まれます。私はおそらくばかげた間違いを犯していますが、私は本当にそれを見ることができません。誰かが見てくれて助けてくれたら、本当にありがたいです。

本当にありがとう!:)

Inputfile sample:
skool   school
losieshuis  pension
prys    prijs
eeu    eeuw
lys lijs
water   water
outoritêr   outoritaire


#!/usr/bin/perl-w
use strict;
use warnings;
use open ':utf8';
use autodie;

open OSWNM, "<SecondWordsNotMatched.txt";
open ONIC, ">Output1NonIdenticalCognates.txt";
open ONC, ">Output2NonCognates.txt";

while (my $line = <OSWNM>)
{
    chomp $line;        
    my @Row = $line;

    for (my $x = 0; $x <= $#Row; $x++)
    {
        my $RowWord = $Row[$x];

#Match: anything, followed by 'y' or 'lê' or 'ê', followed by anything, followed by 
a tab, followed by anything, followed by 'ij' or 'leggen' or 'e', followed by anything

      if ($RowWord =~ /(.*)(y|lê|ê)(.*)(\t)(.*)(ij|leggen|e)(.*)/)
      {
        print ONIC "$RowWord\n";
      }


#Match: anything, followed by 'eeu', followed by 'e' or 's', optional, followed by 
anyhitng, followed by a tab, followed by anything, followed by 'eeuw', followed by 'en', optional

      if ($RowWord =~ /(.*)(eeu)(e|s)?(\t)(.*)(eeuw)(en)?/)
    {
        print ONIC "$RowWord\n";
      }

      else
    {
        print ONC "$RowWord\n";
    }
}
}
4

1 に答える 1

2

ループ内には、基本的に次のものがあります。

if (A) {
  output to file1
}

if (B) {
  output to file1
} else {
  output to file2
}

したがって、 (output to file2満足されたBかどうかAに関係なく) 満たさないものはすべて出力し、 と の両方を満たすものを に出力ABますfile1

2 回出力することが意図されていない場合は、ロジックを次のように変更する必要があります。

if (A or B) {
  output to file1
} else {
  output to file2
}

または:

if (A) {
  output to file1
} elsif (B) {
  output to file1
} else {
  output to file2
}

(この 2 番目のバージョンでは、ABケースに対して異なる処理を行うことができます。)

二重出力が意図されていた場合は、次のようにすることができます。

my $output_to_file2 = 1;

if (A) {
  output to file1
  $output_to_file2 = 0;
}

if (B) {
  output to file1
  $output_to_file2 = 0;
}

if ($output_to_file2) {
  output to file2
}
于 2012-07-01T14:09:34.857 に答える