Cristian Velandia
Cristian Velandia

Reputation: 651

Can I seek and count for different types in a Mathematica result with a Perl script?

Hi I tried to seek three different patterns into a large output file generate in Mathematica. I want to analyze with a perl script. This is the output file:

{{{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}}}

I need to quantity how much {\d,\d,0} and {\d,\d,1} and {\d,\d,2} are found into the input file. My code is this:

#!/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;

The output is:

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

But this is a false result, because the program don´t count all patterns present in the string.

How I modify the program to do this?

Upvotes: 1

Views: 86

Answers (1)

rhuffstedtler
rhuffstedtler

Reputation: 508

There are a couple of oddities in your code.

  1. You are iterating over a scalar $#lines, when you probably mean to be iterating over @lines. Because of the way you have written the inside of the loop, that doesn't have a negative impact. However, if your intent is to process a file with more than one line in a future version, it will.

  2. You are only matching single digit numbers. You probably need to add a quantifier, preferably "+"

  3. If the delimiter inside each group will always be a comma, match specifically on the comman rather than a non-word character. It will make your regex more readable and slightly more efficient.

Here's the code I ended up with. NOTE: I have not verified these counts. I leave it up to you to do that, but issue #2 was the main thing causing your under-reporting.

#!/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;

Upvotes: 3

Related Questions