jrvd has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks,
I have a problem doing something the smart way. I am new to programming in perl so am unsure what the best solution is.
My problem is this: I have a text file that has many lines. I want to go through it to match lines with /^hostname / and once I find a line that contains this, I want to go through the following lines, do some actions on said lines until I find another line that contains /^hostname / and repeat.
Here is what I did so far:
sub hostname{ print $fd $_; while (<$fh>){ if ($_ =~ /^hostname /){ hostname; }else{ # print $_; $tot2++; } } $tot++; } while(<$fh>){ hostname if ($_ =~ /^hostname /); }
Obviously I open and close the files and declare the necessary variables. However, although this works, it gives me a deep recursion warning so I am fairly certain there is a better way to do this that I just haven't thought of.
Thanks for your help!
jrvd
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Too much recursion
by blue_cowdawg (Monsignor) on Jun 27, 2013 at 15:09 UTC | |
by jrvd (Novice) on Jun 27, 2013 at 15:29 UTC | |
by mprentice (Sexton) on Jun 27, 2013 at 15:46 UTC | |
by jrvd (Novice) on Jun 27, 2013 at 15:54 UTC | |
Re: Too much recursion
by pklausner (Scribe) on Jun 27, 2013 at 16:11 UTC | |
by sundialsvc4 (Abbot) on Jun 28, 2013 at 03:09 UTC | |
Re: Too much recursion
by Dallaylaen (Chaplain) on Jun 27, 2013 at 15:53 UTC |