Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Too much recursion

by pklausner (Scribe)
on Jun 27, 2013 at 16:11 UTC ( #1041045=note: print w/ replies, xml ) Need Help??

in reply to Too much recursion

No recursion needed here. This is simple awk-style stream processing like in the (untested!) sample. Re-arrange the sequence of the matching rules to include/exclude the leading/ending patterns for your hostname sections.

while (<>) { /^hostname/ and do { $host_section = 1; ++$tot; # number of hostnames }; if ($host_section) { print $_; ++$tot2; # lines in host sections # do some processing... } /^end-of-hostname-pattern/ and do { $host_section = 0; }; }

Comment on Re: Too much recursion
Download Code
Replies are listed 'Best First'.
Re^2: Too much recursion
by sundialsvc4 (Abbot) on Jun 28, 2013 at 03:09 UTC

    In your code, it seems to me that you would wish to insert a next statement after the line with “number of hostnames” comment, so that the processing of that particular record ends at that point instead of falling-through.

    The general structure of an awk program (this being one of the original inspirations for Perl) is instructive:   the awk language consists of regular-expression patterns followed by code-sections that are executed if a particular pattern is matched.   (Other special sections are executed at the beginning and at the end of the file.)   This encourages one to approach problems like these by first identifying each “type of line” that the file might contain ... building a regular expression for each ... then deciding what to do with each one.   In this example, there are at least two types:   “is a hostname line,” and “isn’t.”

    Recursion isn’t needed to solve problems like these.   For dealing with very complicated inputs, the notion of a Finite-State Machine (FSM) can be useful.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041045]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2015-10-13 19:31 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (314 votes), past polls