1

嗨,我试图在 Mathematica 中生成的大型输出文件中寻找三种不同的模式。我想用 perl 脚本分析。这是输出文件:

{{{4,1,2},0,{2,2,2},{4,4,2},{3,5,2},{4,11,0},0,{3,6,2},0,{4,7,2}},{{3,8,2},{1,22,0},   {3,3,2},{1,16,0},{1,9,0},{2,10,0},{3,74,1},{2,18,2},{3,12,0},{2,13,0}},{{1,14,0},0,{2,15,0},0,{1,17,0},0,0,{4,19,1},{4,20,0},{3,30,0}},{{3,38,0},{2,21,0},{1,23,0},{1,24,0},{3,25,0},{1,26,0},{2,27,0},0,{1,28,0},{2,29,0}},{0,{3,32,0},{4,33,0},0,{3,41,0},{3,34,0},{3,36,0},{4,43,0},{4,37,0},{2,31,0}},{{4,46,0},{3,39,0},{1,40,0},{4,49,0},{1,35,0},{3,42,0},{4,44,0},0,0,{4,45,0}},{{2,47,0},0,{1,48,0},{1,57,0},{1,50,0},{1,51,0},{1,60,0},{3,52,0},0,{2,53,0}},{{1,54,0},{2,55,0},0,{1,56,0},{2,58,2},0,{4,59,2},0,{2,68,0},{3,69,0}},{0,{4,62,0},{4,71,2},{1,63,0},{3,64,2},0,{1,65,2},{4,67,0},0,{4,61,2}},{{2,76,2},{2,70,1},0,0,{4,72,2},{4,73,2},0,0,{3,66,2},{1,75,2}}}

我需要计算在输入文件中找到了多少 {\d,\d,0} 和 {\d,\d,1} 和 {\d,\d,2} 。我的代码是这样的:

#!/usr/bin/perl

use strict;
use warnings;

my $count0 = 0;
my $count1 = 0;
my $count2 = 0;
my $totalcount = 0;

open(MYINPUTFILE, "ultimo.in"); # open for input
my @lines = <MYINPUTFILE>; # read file into list

foreach ($#lines) { # loop thru list
    $count0++ while $lines[0] =~ /\{\d\W\d\W0\}/g;
    $count1++ while $lines[0] =~ /\{\d\W\d\W1\}/g;
    $count2++ while $lines[0] =~ /\{\d\W\d\W2\}/g;
    $totalcount++ while $lines[0] =~ /\{\d\W\d\W\d\}/g;
    }       

print "\n"."Esta es la cuenta de Suceptibles0 es :"."$count0"."\n";
print "\n"."Esta es la cuenta de Infectados1 es :"."$count1"."\n";
print "\n"."Esta es la cuenta de Recuperados2 es :"."$count2"."\n";
print "\n"."El total de agentes en esta grilla es:"."$totalcount"."\n";


close(MYINPUTFILE);

exit;

输出是:

Esta es la cuenta de Suceptibles0 es :1

Esta es la cuenta de Infectados1 es :0

Esta es la cuenta de Recuperados2 es :8

El total de agentes en esta grilla es:9

El total de agentes en esta grilla es:0

El total de agentes en esta grilla es:0

但这是一个错误的结果,因为程序不会计算字符串中存在的所有模式。

我如何修改程序来做到这一点?

4

1 回答 1

3

您的代码中有几个奇怪的地方。

  1. 当您可能要迭代 @lines 时,您正在迭代标量 $#lines。由于您编写循环内部的方式,这不会产生负面影响。但是,如果您的意图是在将来的版本中处理包含多行的文件,它会的。

  2. 您只匹配单个数字。您可能需要添加一个量词,最好是“+”

  3. 如果每个组内的分隔符始终是逗号,则专门匹配逗号而不是非单词字符。它将使您的正则表达式更具可读性并且效率更高。

这是我最终得到的代码。注意:我尚未验证这些计数。我让你来做这件事,但问题 #2 是导致你报告不足的主要原因。

#!/usr/bin/perl

use strict;
use warnings;

my $count0 = 0;
my $count1 = 0;
my $count2 = 0;
my $totalcount = 0;

open(MYINPUTFILE, "ultimo.in"); # open for input
my @lines = <MYINPUTFILE>; # read file into list

foreach (@lines) { # loop thru list
    $count0++ while $lines[0] =~ /\{\d+,\d+,0\}/g;
    $count1++ while $lines[0] =~ /\{\d+,\d+,1\}/g;
    $count2++ while $lines[0] =~ /\{\d+,\d+,2\}/g;
    $totalcount++ while $lines[0] =~ /\{\d+,\d+,\d\}/g;
    }       

print "\n"."Esta es la cuenta de Suceptibles0 es :"."$count0"."\n";
print "\n"."Esta es la cuenta de Infectados1 es :"."$count1"."\n";
print "\n"."Esta es la cuenta de Recuperados2 es :"."$count2"."\n";
print "\n"."El total de agentes en esta grilla es:"."$totalcount"."\n";


close(MYINPUTFILE);

exit;
于 2012-07-14T22:47:58.537 回答