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

  • Home
  • SEARCH
  • 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 7516219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:03:51+00:00 2026-05-30T01:03:51+00:00

I feel like I am missing something very simple here, but this is the

  • 0

I feel like I am missing something very simple here, but this is the first time I’ve needed to do this and am having trouble finding an example.

I have a giant foreach loop that walks through output logs and extracts various bits of information based on matching regular expressions. My main problem is that a few larger types of output have a header and footer, like *** Begin bangle tracking log*** followed by several lines of gibberish and then a ***End bangle tracking log***.

Is there a way, from within a foreach loop, to have an inner loop that stores all the lines until a footer is found?

foreach my $line( @parseme )
{
    if( $line =~ m/***Begin bangle tracking log***/ )
    {
        #Help! Push all lines into an array until bangle tracking footer is found.
    }
    if( $line =~ m/Other stuff I am tracking/ )
    {
        #Do other things
    }
}
  • 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-30T01:03:52+00:00Added an answer on May 30, 2026 at 1:03 am

    You could use the range operator, which acts as a flip-flop in scalar context:

    foreach ( @parseme ) {
        if ( /Begin bangle tracking log/ .. /End bangle tracking log/ ) {
            push @array, $_;
        }
        # other stuff...
    }
    

    I used $_ for the foreach loop because it allows for more concise syntax. You can use another variable if you like, but then you’ll have to write the condition as something like:

    if ( $line =~ /Begin .../ .. $line =~ /End .../ ) {
    

    which might be more readable with some extra parentheses:

    if ( ($line =~ /Begin .../) .. ($line =~ /End .../) ) {
    

    One issue to note about the flip-flop operator is that it remembers its state even after the loop ends. This means that, if you intend to run the loop again, you really ought to make sure that the @parseme array ends with a line that matches the /End .../ regexp, so that the flip-flop will be in a known state when the loop starts the next time.

    Edit: Per DVK’s comment below, if you want to process the collected lines as soon as you reach the footer line, you can do that by checking the return value of the .. operator, which will end with E0 on the last line:

    foreach ( @parseme ) {
        my $in_block = /Begin bangle tracking log/ .. /End bangle tracking log/;
        if ( $in_block ) {
            push @array, $_;
        }
        if ( $in_block =~ /E0$/ ) {  # last line
            # process the lines in @array
            @array = ();
        }
        # other stuff...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like I'm missing something here, but I have this datagrid which when
I feel like I'm missing something very simple here. I have Eclipse set up
I feel like this is easy but I am missing something... Using jQuery, I
I feel like this is a dumb question and I'm missing something, but I
I feel like this should be a no brainer, but clearly I'm missing something...
I feel like im missing something simple here. I am using the form_for helper
I feel like I must be missing something completely obvious, but I don't see
I feel like the answer to this question is really simple, but I really
I give up. Hopefully I am just missing something easy, but I feel like
I might be missing something basic here, but I'm stumped on this error: model

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.