Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:45:38+00:00 2026-06-07T15:45:38+00:00

Hi I tried to seek three different patterns into a large output file generate

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:45:39+00:00Added an answer on June 7, 2026 at 3:45 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
Tried to make a little old school ajax (iframe-javascript) script. A bit of mootools
I'm trying to append some data to a file, but in some cases want
I want to read a line in a file and insert the new line
I tried to use the read/write file descriptor in bash so that I could
I'm reading specific lines from a text file. Here is my code: file.seek(0) for
I'm trying to copy a chunk from one binary file into a new file.
I want to convert a HTML page into MS word. I want to know
I tried to write a file monitor which will check the file if a
I have a Zip archive with a large (important) file that will not extract.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.