in reply to Loop and Add more cookies
What's $COOKIES? Variable names are case sensitive, so it's not the same as $cookies. Use strict and warnings to be safe.
To increment a variable by 1, use just $var++, no assignment there.
exit is not needed at the very end of the script. What else could perl do at the end of the source code?
If you compare how the string in the condition is built with the string that's printed, you'll notice small differences. It's better to only have one place to create the string - a subroutine. Avoid copy and paste, it's hard to maintain. Maybe like this:
#!/usr/bin/perl use warnings; use strict; sub cookies { my $number = shift; return ('COOKIES ' x ($number - 1) . 'COOKIES' . 'S' x $number) } my $number = 1; my $cookies = ""; while ($cookies ne cookies($number)) { $number++; print 'Cookie monster: I want ', cookies($number), "!\n"; chomp($cookies = <STDIN>); } print "Mmmm. $number COOKIES. :-)\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Loop and Add more cookies
by Accelerator (Initiate) on Oct 12, 2015 at 18:18 UTC | |
by choroba (Cardinal) on Oct 12, 2015 at 20:30 UTC | |
by Accelerator (Initiate) on Oct 12, 2015 at 20:57 UTC | |
|
Re^2: Loop and Add more cookies
by Accelerator (Initiate) on Oct 12, 2015 at 15:42 UTC | |
by choroba (Cardinal) on Oct 12, 2015 at 15:48 UTC | |
by Accelerator (Initiate) on Oct 12, 2015 at 17:51 UTC |