0

我有两个文件:file1.txtfile2.txt. 两者都包含以下格式的行:

文件 1

name1:value1

文件2

name2:value2

我想检查是否value1list2(在name2字符串中)找到

我有这个功能:

#!/usr/bin/perl
use warnings;
use Parallel::ForkManager;

sub loadf($);

print "Starting main program\n";
my @list1 = loadf("list1.txt");
my @list2 = loadf("list2.txt");

my $workernum = 10; 
open(OK, '>>', 'valid.txt');
open(ER, '>>', 'invalid.txt');
$pm = new Parallel::ForkManager($workernum);

my $cnt = 0;
foreach $line (@list1) {
    $cnt++;
    my $pid = $pm->start and next;
    my @data1 = split(":", $line);
    my $name1 = $data1[0];
    my $value1 = $data1[1];
    my @data2 = split(":", $list2);
    my $name2 = $data2[0];
    my $value2 = $data2[1];

    if (/$value1/i ~~ @list2) 
    {
        print OK $name1 . " - " . $value2 . "\n";
        print " [+] Found: " . $name1 . " - " . $value2 . "\n";
    }
    else
    {
        print ER $name1 . "\n";
        print " [x] Unknown: " . $name1 . " - " . $value1 . "\n";
    }
    $pm->finish;
}

close(OK);
close(ER);
print "\n*** Finished ***\n";

sub loadf($) {
    my @file;
    open(FILE, $_[0] . "\n") or die("[+] Couldn't open " . $_[0] . "\n");
    @file = <FILE>;
    close(FILE);
    return @file;
}

__END__

这是行不通的。我究竟做错了什么 ?

4

2 回答 2

1
#!/usr/bin/perl

open(F,'list1.txt');
my @list1=<F>;
close(F);
open(F,'list2.txt');
my @list2=<F>;
close(F);
chomp(@list1,@list2);
foreach my $line (@list1)
{
    if ($line=~/.+\:.+/)
    {
        my @data1 = split(":", $line);
        if (my @d2=grep /$data1[1]\:/i,@list2){print " [+] Found: " . $data1[0] . " - " . [split(':',$d2[0])]->[1] . "\n";   }
        else { print " [x] Unknown: " . $data1[0] . " - " . $data1[1] . "\n"; }
    }
}
于 2012-04-30T09:12:23.080 回答
1

呃...我真的不明白你的算法,对不起。首先,您将比较文件中的所有行读入两个数组,然后分叉处理器循环,但正如我所见,没有尝试将这些工作人员的工作分成块(以并行处理)。

我建议尝试一些不同的方法:只吃第二个文件,然后逐行处理第一个文件。您没有提到第二个文件的名称和值是否唯一;我想他们不是,但如果他们是,程序可以变得更简单。

open my $caf, '<', 'list2.txt' or die $!, "\n";
my $checkedAgainst = do { local $/;  <$caf>; };

open my $cf, '<', 'list1.txt' or die $!, "\n";

my $workernum = 10; 
$pm = new Parallel::ForkManager($workernum);

while (<$cf>) {
  my $pid = $pm->start and next;
  my ($nameToCheck, $valueToCheck) = split /:/;
  if ($checkedAgainst =~ /^\Q$valueToCheck\E:(.+)$/m) {
    print " [+] Found: $nameToCheck - $1", "\n";
  }
  else {
    print " [x] Unknown: $nameToCheck - $valueToCheck", "\n";
  }
}
$pm->finish;

换句话说,我首先将第二个文件加载到一个大字符串中,然后尝试将它与第一个文件中的行匹配(逐行)。我不知道,你的值中会出现什么符号,这就是为什么在那里使用 \Q-\E (quotemeta 运算符)。

更新:试图制作这个代码分支,但我无法在我所在的地方测试它。

于 2012-04-30T09:03:46.433 回答