Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

do-until inside while loop control

by mtorba (Novice)
on Nov 23, 2015 at 23:48 UTC ( [id://1148450]=perlquestion: print w/replies, xml ) Need Help??

mtorba has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I did not figure out how to solve this question after take a look in the main discussion lists.

I have a data_file like this one:

regex1 - grab general informations about regex1

grab more informations on this line about regex1

grab more informations on this line about regex1

grab more informations on this line about regex1

grab more informations on this line about regex1

grab more informations on this line about regex1

...

grab more informations on this line about regex1

regex2 - grab general informations about regex2

grab more informations on this line about regex2

grab more informations on this line about regex2

grab more informations on this line about regex2

grab more informations on this line about regex2

grab more informations on this line about regex2

...

grab informations on this line

regex3 - grab general informations about regex3

grab more informations on this line about regex3

grab more informations on this line about regex3

grab more informations on this line about regex3

grab more informations on this line about regex3

grab more informations on this line about regex3

...

EOF

I'm using a do-until inside a while loop:

while<data_file> { if(/regex1/) { get some general informations about regex1 do { get more informations in lines between regex1 e regex2 } until(/regex2/) } }
It works, but it returns to while loop in the first line after regex2. So, I cannot check for regex2 and get general informations about it. Thanks for any help.

Replies are listed 'Best First'.
Re: do-until inside while loop control
by kcott (Archbishop) on Nov 24, 2015 at 04:21 UTC

    G'day mtorba,

    Welcome to the Monastery.

    This sounds like a job for the Range Operator. Perhaps something like this:

    #!/usr/bin/env perl use strict; use warnings; while (<DATA>) { if (/^regex1/ .. /^regex3/) { last if /^regex3/; print; } } __DATA__ PRE REGEX LINES regex1 - grab general informations about regex1 grab more informations on this line about regex1 regex2 - grab general informations about regex2 grab more informations on this line about regex2 regex3 - grab general informations about regex3 grab more informations on this line about regex3 POST REGEX LINES

    Output:

    regex1 - grab general informations about regex1 grab more informations on this line about regex1 regex2 - grab general informations about regex2 grab more informations on this line about regex2

    While I appreciate this is your first post, it is lacking in several areas (including runnable code, actual output, expected output, etc.) and responses will reflect this. A better question will get you a better answer. Please see "How do I post a question effectively?" for ways to improve future posts.

    — Ken

Re: do-until inside while loop control
by Paladin (Vicar) on Nov 24, 2015 at 00:34 UTC
    Check out the redo command, which allows you to go back to the top of a loop without re-evaluating the conditional (in your case, without reading another line in while (<data_file>)) so $_ will still contain the line with regex2 in it.
Re: do-until inside while loop control
by GrandFather (Saint) on Nov 23, 2015 at 23:58 UTC

    Give us some code we can run with a minimum of code and data required to show the issue. See I know what I mean. Why don't you? for hints.

    Premature optimization is the root of all job security

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1148450]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-28 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found