0

配列に読み取ったテキストドキュメントに保存された行があります。ラインは

John is the uncle of Sam

auntuncleおよびという単語を含む別の配列がありfatherます。両方の配列を比較して叔父を出力したいと思います(大文字と小文字を区別しません)。何が間違っているのかわかりません。List::CompareArray::Utils qw(:all)などを使用しました。誰かが動作するコードを教えてくれませんか。必要なのは比較部分だけです。

これが私がこれまでに行ったすべてです。

#!/usr/bin/env perl

use strict;
use warnings;
use Array::Utils qw':all';

print "Please enter the name of the file\n";
my $c = <STDIN>;
chomp($c);

open(NEW,$c) or die "The file cannot be opened";

my @d = <NEW>;


my @g = qw'aunt uncle father';
chomp(@g);
chomp(@d);

my @isect = intersect(@g, @d);
print @isect;
4

2 に答える 2

2

最も簡単に:

for my $line (@file) {
    for my $word (@words) {
        if ($line =~ /\Q$word/i) {
            print "$word is found in '$line'";
        }
    }
}

単語を正規表現にマージすると、単語のループをスキップできます。

my $rx = join '|', map quotemeta, @words;
for my $line (@file) {
    if ($line =~ /$rx/i) {
        print "Match found";
    }
}

または使用grep

my @found = grep /$rx/i, @file;
于 2013-08-06T11:47:16.450 に答える