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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:07:00+00:00 2026-06-15T09:07:00+00:00

The question I have is a bit abstract, but I’ll attempt to be clear

  • 0

The question I have is a bit abstract, but I’ll attempt to be clear in my statement. (This is something of a “rubber duck effect” post, so I’ll be thankful if just typing it out gets me somewhere. Replies, however, would be brilliant!)

I have old fortran code that I can’t change (at least not yet) so I’m stuck with its awkward output.

I’m using perl to post-process poorly annotated ascii output files, which, as you might imagine, are a very specialized mixture of text and numbers. “Ah, the perfect perl objective,” you say. Yes, it is. But what I’ve come up with is pretty terrible coding, I’ve recently concluded.

My question is about the generic structure that folks prefer to use to achieve such an objective. As I’ve said, I’m not happy with the one I’ve chosen.

Here’s some pseudocode of the structure that I’ve arrived at:

flag1 = 0;
flag2 = 0;
while (<INPUT>) {
   if (cond1) {
      do something [like parse and set a header];
      flag1 = 1;
   } else {
     next;
   }
   if (flag1 == 1 && cond2) {
      do something else [like process a block of data];
   } else {
     next;
   }
}

The objective of the above code is to be able to break the processing into blocks corresponding to the poorly partitioned ascii file — there isn’t much by way of “tags” in the file, so the conditions (cond1, cond2, etc.) are involved. The purpose of having the flags set is, among other reasons, to track the progress of the code through the file.

It occurs to me now that a better structure might be

while (<INPUT>) {
   do stuff;
}

while (<INPUT>) {
   do other stuff;
}

In any event, if my rambling inspires any thoughts I’d appreciate hearing them.

Thank you

  • 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-15T09:07:00+00:00Added an answer on June 15, 2026 at 9:07 am

    Your original structure is perfectly fine. You’re building a state machine, and doing it in a completely reasonable way that can’t really be made any more idiomatic.

    The only thing you can possibly do if you wish is to modularize the code a wee bit:

    our %state = (last => 0, current => 0, next => 0);
    our %extra_flags = ();
    sub cond1($line) { return $next_state } # Returns 0 if cond==false
    sub cond2($line) { return $next_state } # Returns 0 if cond==false
    
    our %conditions = (
       0 => \&cond1
       1 => \&cond2  # flag1 is set
    );
    
    while (<INPUT>) {
       my $state = $state->{current};
       if ($state->{next} = $conditions{$state}->($_, $state)) {
          $do_stuff{$state}->{$next_state}->($line);
          $state->{last} = $state->{current};
          $state->{current} = $state->{next};
          next;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question might seem a bit confusing but I have an if statement that
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
The question plot (a bit abstract, but answering this question will help me in
This will be a bit abstract but I have a method like this: private
I have little bit longer question for you - but hope answer will be
Following this question and answer , I still have a bit trouble in the
Bit of a noob question but: I have a custom class defined as: public
Bit of an unusual question, but I have setup a field inside a MySQL
I'm not even sure how to begin wording this question, but basically, I have
I'm well aware that variants of this question have been asked before, but I

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.