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 7543079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:20:05+00:00 2026-05-30T08:20:05+00:00

I am doing some text parsing using flip-flop operator and my data looks like

  • 0

I am doing some text parsing using flip-flop operator and my data looks like below:

COMMAND START

CELL
123

COUNTER1    COUNTER2    COUNTER3
23          25          45

COUNTER1    COUNTER2    COUNTER3
22          34          52

CELL
234

COUNTER1    COUNTER2    COUNTER3
12          35          35

END

Now i need to iterate through this data and for each CELL, find the Sum of COUNTER2. Under each cell as we can see there might be 1 or more COUNTER rows.
I tried using flip-flop operator like below, but its not working.

my ($CELL_ID_COL1, $TEMP_COUNT);
my @line_contents;

while (<>) {
    chomp;
    if(/^COMMAND.*START$/ .. /^END$/) {

        if (my $e1 = /^CELL$/ ... (/^CELL$/ || /^END$/)) {
            if ($e1 == 2) {
                @line_contents = split(/\s+/, $_);  #Split the line read on whitespaces
                $CELL_ID_COL1 = $line_contents[0];
                print "$CELL_ID_COL1\n";
                $TEMP_COUNT = 0;
            }

            if (my $e2 = /^COUNTER1.*COUNTER3$/ ...(/^COUNTER1.*COUNTER3$/ || /^CELL$/ || /^END$/) ) {
                print "$_\n";
                if ($e2 ==2) { 
                    @line_contents = split(/\s+/, $_);  #Split the line read on whitespaces
                    $TEMP_COUNT += $line_contents[1];
                }
                if ($e2 =~ /E0$/) {
                    redo;
                }

            }
            if ($e1 =~ /E0$/) {
                print "FINAL COUNT is: $TEMP_COUNT\n";              
                redo;
            }

        }
    }
}

I think this got to do something with the global state of flip-flop operator discussed in this question, but could not understand much. Please help me.

Thanks a lot in advance.

  • 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-05-30T08:20:06+00:00Added an answer on May 30, 2026 at 8:20 am

    I think it’s easier without the flip flop. Sometimes you start off thinking a feature is the way to go, but then you run into problems. Instead of resisting the urge to stick with the feature until you force it into the round whole, consider a completely different way to do it”

    use Data::Dumper;
    use List::Util qw(sum);
    
    my %cells;
    my $cell_id;
    my $started;
    
    while( <DATA> ) {
        $started++ if  /COMMAND START/;
        next unless $started;
        next if /\A\s*\z/;
        last if /END/;
    
        if( /CELL/ ) {
            chomp( $cell_id = <DATA> );
            $index = 0;
            }
    
        if( /COUNTER/ ) {
            my @counters = ( <DATA> =~ /([0-9]+)/g );
            $cells{$cell_id}[$index++] = \@counters;
            }
        }
    
    my %sums;
    foreach my $cell_id ( keys %cells ) {
        $sums{$cell_id} =  sum map { $_->[1] } @{$cells{$cell_id}}
        }
    
    print Dumper( \%cells, \%sums );
    

    You can also modify this to accumulate the sums as you go.

    However, if you still want to use the flip flop, you can read Respect the global state of the flip flop operator and Make exclusive flip-flop operators

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My particular scenario involves doing some text transformation using regular expressions within a private
I'm trying to use Antlr for some text IDE-like functions -- specifically parsing a
While doing some refactoring I've found that I'm quite often using a pair or
Been doing some playing call my service which is on a different domain using
I've got some HTML like the following: <span id=A>Text I'm interested in <span id=B>Other
im doing parsing and the kind of text that i want to match and
Using Qt 4.7.0, we need some text to display part way between normal and
I'm doing some txt->image generating using convert of ImageMagick. But it outputs only rasterized
Doing some jquery animation. I have certain divs set up with an attribute of
After doing some work with Ruby, Rails, and RSpec last summer and I learned

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.