嗨,我试图在 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
但这是一个错误的结果,因为程序不会计算字符串中存在的所有模式。
我如何修改程序来做到这一点?