Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Too much recursion

by blue_cowdawg (Monsignor)
on Jun 27, 2013 at 15:09 UTC ( #1041035=note: print w/replies, xml ) Need Help??


in reply to Too much recursion

      However, although this works, it gives me a deep recursion

I'm not surprised. The question this begs to me is why the recursion. Something like this should suffice:

| hand waving here. while(<$fd>){ next if $_ =~ m/^hostname /; print $_; $tot2++; } $tot++
Now, what is troubling me is when or how your re initializing the vars $tot2 and $tot. That would change my looking logic ever so slightly if I knew that.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Replies are listed 'Best First'.
Re^2: Too much recursion
by jrvd (Novice) on Jun 27, 2013 at 15:29 UTC

    Hi,

    Thanks for your help. I have put my entire code so that you may see where my variables were initialized.

    #!/usr/bin/perl use strict; use warnings; my $fileName = "version.txt"; my $fileDest = "version_modified.txt"; die "Problème d'ouverture du fichier $fileName" unless open(my $fh, '< +', $fileName); die "Problème de création du fichier $fileDest" unless open(my $fd, '> +', $fileDest); my $tot = 0; my $tot2 = 0; sub hostname; sub hostname{ print $fd $_; while (<$fh>){ if ($_ =~ /^hostname /){ hostname; }else{ # print $_; $tot2++; } } $tot++; } while(<$fh>){ hostname if ($_ =~ /^hostname /); } print $tot."\n"; print $tot2."\n"; close $fh; close $fd;

    I am not sure if the code you suggested should only replace a certain part. I might not have correctly expressed what I was looking for however. What I need to do is find the line containing "hostname ", print that, then do some various modifications on the following lines until I find another "hostname " and repeat. Right now, all I was doing was incrementing $tot2 until I got this to work but eventually this will change.

    Thanks!

    jrvd

      Seems like you can do the same without a recursion:

      Update: Add "print $fd $_" to preserve original behavior.

      sub hostname{ print $fd $_; while (<$fh>){ if ($_ =~ /^hostname /){ print $fd $_; $tot++; next; }else{ $tot2++; } } $tot++; # keep initial increment }

        Works exactly the way I need it! Thanks!

        jrvd

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041035]
help
Chatterbox?
[Discipulus]: Corion are you would able to realize such thing? O_O
[Corion]: In the same vein I have a script that automates Firefox to enter some data into another system. It's not faster than the people using the script if they were to do it manually, but they prefer not having to check the data and not having typos when ...
[Corion]: ... entering the data
[Corion]: Discipulus: I don't know whether I could really do that, but the init process itself mostly launches other processes, and the whole startup is just following a path of dependencies and making sure they are all running. Which basically is what ...
[Discipulus]: when at work my time is (temporarly) owned by the firm, so i do not care (coworkers whatch movies.. I code Perl)
[Corion]: ... make already does, except for files instead of programs. But maybe with some /proc hackery, that could be eliminated and one could use plain make :-D
[choroba]: systemd just makes is asynchronous
[choroba]: so, make -j
[Corion]: Discipulus: Yeah - but when writing Perl to save time (instead of having fun), it helps to look whether you're actually saving time ;) Why spend 5 minutes doing manually what you can spend three years automating? ;)
[Corion]: choroba: Oh, yeah :-D

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2017-07-27 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (408 votes). Check out past polls.