-2

我有一个用来匹配另一个表的数组。当我执行它时,它只抓取第一次出现。例如,如果 company1 在我的数组中,它将只抓取 company1 的第一个实例,然后转到下一个搜索词,比如 company2。如果company1之后有company1.0,则只会吐出company1。我希望它在同一行吐出 company1 etc.\t company1.0 等等,因为两个列表之间会有多个匹配项。

这是我的代码:

my @attendees = ('company');
foreach $fbm (@attendees) {
    open(RFILE, '<', "file.txt")
    or die "no such file posf: $!";

    while ( $line = <RFILE> )
    { 
        if ($line =~ /$fbm/i)
        {
            print $fbm."\t". $line;
            last;
        }
        if (eof(RFILE)) 
        {
            print "posf"."\n";
        }               
    }
}
print STDERR "\n\nFINISHED!!";

我的输入:

company1
company1.0
company1 also begins with 1 but different ending
company1 can i have this one too?

我的输出:company1 所需的输出:company1\tcompany1.0\tcompany1 也以 1 开头,但结尾不同\tcompany1 我也可以有这个吗?

4

1 回答 1

0
my @attendees = ('company');
my @whatever;
open ( my $fh, '<', "file.txt")
    or die "could not open file.txt: $!";

while ( <$fh> ) {
    chomp $_;
    push @whatever, $_;
}

foreach my $attendee ( @attendees ) {
     foreach my $thing ( @whatever ) { 
        if ($thing =~ /$attendee/i) {
            print $attendee, "\t", $thing, "\n";
        }
    }
}
print STDERR "FINISHED!\n";

也许这可以满足您的要求,但我必须承认我不太确定。

于 2013-06-17T19:27:48.577 回答