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

well here is the problem, this doesn't work. i want to choose if i want to continue the recursion or not but with different input file , example:
use strict; print "do you want to continue(y\/n):"; chomp (my $answer = <>); my $name = "foo.txt"; if ($answer=~/y/i){ my $first = "bar.txt"; Edit($first); } elsif ($answer =~/n/i){ exit } ################################### sub Edit{ my $nod = shift; ## so here i enterd the file i want to open open (NOD, "<", $nod)|| die "$!"; open (NAMES, "<", $name) || die "$!"; open (OUT, ">", "out.txt") || die "$!"; ...some proces... close NOD; close NAMES; close OUT; print "do you want to continue(y\/n):"; chomp (my $change = <>); if ($change =~/y/i){ print "what you wish to change:"; chomp (my $cho = <>); if ($cho){ open (SEC, "<", $nod)|| die "$!"; open (NEW, ">", "simon.txt") || die "$!"; ... do something... print NEW "$_"; } close NEW; open (ADD, ">>", "simon.txt")|| die "$!"; print ADD "\| -- -- --\|\n"; close ADD; } elsif ($change =~/n/i){ exit } print "one more time(y\/n):"; chomp(my $a =<>); if ($a =~/y/i){ my $rr = "simon.txt"; #### and here if i choose to continue, want +to redo the proces but this time with this new file Edit($rr); } elsif ($a =~/n/i){ last } }
so any help would bi nice. of course i tried to do it through redo procedure but i could'n pass the new variable and redo the process with it. thnx

Replies are listed 'Best First'.
Re: loop problem
by linuxer (Curate) on Jul 04, 2008 at 22:40 UTC

    Why do you want to leave a loop in line 78 (last)? There is no loop!

    Why can't you pass the new variable and redo the process? What happens instead?

    Took your code and cleaned it up a bit. Looks as if it works as designed...

    #!/usr/bin/perl # vi:ts=4 sw=4 et: use strict; use warnings; print "continue [y/N]? "; chomp ( my $answer = <STDIN> ); # any other answer than 'y' or 'Y' results in an termination of the pr +ogram if ( $answer !~ m/[Yy]/ ) { exit 0; } # process first foo( 'foo.txt' ); sub foo { my $file = shift; print "work with '$file'\n"; print "continue [y/N]? "; chomp( $answer = <STDIN> ); if ( $answer !~ m/[yY]/ ) { exit 0; } print "change what? "; chomp( my $to_change = <STDIN> ); if ( defined $to_change ) { print "wanted to change: $to_change\n"; } print "again [Y/n]? "; chomp( $answer = <STDIN> ); if ( $answer !~ m/[Yy]/ ) { exit 0; } print "running again: \n"; # process second foo( 'bar.txt' ); }
Re: loop problem
by jethro (Monsignor) on Jul 04, 2008 at 22:53 UTC
    You never say what isn't working or what error message you are getting. I had to invest a few minutes just to find out what you could have easily told us

    So you use recursion to simulate a loop (nothing wrong with that, TMTOWTDI). But you try to exit your recursion with last. And the error message just tells you that there is no loop to 'last' out of. Use return instead or as you already do just don't call Edit() again when you want to stop the recursion.

    So the only thing wrong with your program is a useless 'last' that you can delete and then your program works

Re: loop problem
by oko1 (Deacon) on Jul 05, 2008 at 03:30 UTC

    Others have explained the main problem; I want to point out a problem that you didn't ask about but is nevertheless present in your code.

    if ($answer=~/y/i){ my $first = "bar.txt"; Edit($first); } elsif ($answer =~/n/i){ exit }

    Think about what happens if the answer is "Nay", or "Go ahead, do it!" - you have either the opposite result from what you intended, or you completely fall through your tests. Consider trying something like this instead:

    { print "Do you want to continue([yn]): "; chomp(my $answer = <STDIN>); Edit("bar.txt") if $answer =~ /^y/i; exit if $answer =~ /^n/i; redo; }

    My point here is that your tests should be definitive: either the user gives you input within the range that you've defined as valid, or you send him back to the starting gate. Anything else is a long walk over a short pier, code-wise.

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells