Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Subroutine Loop

by tryingnottofailmytes (Novice)
on Dec 06, 2015 at 16:30 UTC ( #1149514=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys. Sorry to bother you again. I've been working on this for an hour trying to get the loop in the subroutine to loop five times. Its only running the main program. I think the probelm is somewhere either in the calling or the names of the arguments. Any help is appreciated. I know this stuff is super simple and annoying to answer for the newbs.
my $messagetouser= "What is your favorite Color?"; my $ans = tryagain5 ( $messagetouser ); if ($ans) { print "$ans\n"; } else { print "You didn't reply! Goodbye.\n"; exit; } #print $answer, "\n"; sub callandresponse { my ($messagetouser)=@_; print $messagetouser,"\n"; my $ans=<STDIN>; chomp $ans; return $ans; } sub tryagain5 { my($messagetouser)=@_; my $ans = callandresponse ($messagetouser); my $count=1; while ($count < 5 and $ans =~ /^\s*\n/) { $count++; $ans = callandresponse ($messagetouser); } if ($ans =~ /^\s*\n/) { return 0; } else { return $ans;} }
I really can't figure out why the loop isn't running......I know the answer is simple but I just can't figure it out.

Replies are listed 'Best First'.
Re: Subroutine Loop
by poj (Abbot) on Dec 06, 2015 at 16:39 UTC
    Since you have chomp $ans; try
    while ($count < 5 and $ans =~ /^\s*$/)
    poj
Re: Subroutine Loop
by james28909 (Deacon) on Dec 06, 2015 at 16:58 UTC

    I think your are chomping $ans in sub callandrespond and then testing it with sub tryagain5 while ($count < 5 and $ans =~ /^\s*\n/) which will fail I am pretty sure because you are testing if the string has a newline at the end, which it never will because you are chomping it.

Re: Subroutine Loop
by Anonymous Monk on Dec 06, 2015 at 16:58 UTC
    "The most effective debugging tool is still careful thought, coupled with judiciously placed print statements."
    Brian Kernighan, "Unix for Beginners" (1979)

    Still true in 2015.

    sub tryagain5 { my ($messagetouser) = @_; my $ans = callandresponse($messagetouser); my $count=1; # DEBUG print "ans is <<$ans>>\n"; print "count is <<$count>>\n"; while ($count < 5 and $ans =~ /^\s*\n/) { $count++; $ans = callandresponse ($messagetouser); } if ($ans =~ /^\s*\n/) { return 0; } else { return $ans; } }

    You should learn how to indent your programs properly: use perltidy. Bad indentation (like yours) is a serious hindrance to careful thought.

Re: Subroutine Loop
by QuillMeantTen (Friar) on Dec 06, 2015 at 16:59 UTC

    Greetings,

    Problem seems to be with your regex in the loop: you only continue if the answer matches 0 or more whitespaces! also since you chomp your input it wont match the newline character. here is a fix

    Also, believe me using strict and warnings will save you a world of hurt!

      Thanks everyone! I didn't realize that reg ex wasn't reading the whitespace b/c said It hand to end with a new line. also rest assured that I AM USING -W AND USE STRICT; I just have a lot of programs in one file and didn't copy it in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2022-06-29 19:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?