in reply to Loop and Add more cookies

I'd like to help you get it done, but it's unclear what you're trying to do. So, here's some general advice:

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

    For better understanding I'm just going to tell you simple

    Our teacher gave us this script and we should figure out the answer -> what to type so the loop would end-> in the command prompt, without changing the script. (We had programming just twice, so i am a total noob). I am German therefor the script is in German (what is actually stupid if you teach an English programming language...), so i decided to transcribe it into English to simplify and to not confuse any of you, but it seems i wasn't clear enough, plus i made some mistakes while changing.

    And so i hope you can help me out:)

    #!/usr/bin/perl -w # # Krümelmonster $ZAHL = 1; $kekse = ""; while ( $kekse ne "KEKSE"x$ZAHL."E " x $ZAHL, ) { $ZAHL =++$ZAHL; print "Kruemelmonster: Ich will"." KEKSE"x$ZAHL."E" x $ZAHL, "!\n"; chomp($kekse = <STDIN>); } print "Mmmm. $ZAHL KEKSE. :-)\n"; exit;
      After the first input, $ZAHL is 2, because
      $ZAHL =++$ZAHL

      increases its value. I'm not sure why your teacher showed you this construct, as there are many easier ways how to add one to a variable:

      $x++; ++$x; $x += 1; $x = $x + 1;

      Therefore, the input is compared to

      "KEKSE" x 2 . "E " x 2

      i.e.

      KEKSEKEKSEE E ~~~~~ ~~ once E + space ~~~~ twice ~~ E + space
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Thank you so much and i even understand how you got to that point, man you made my day, just glad to finally understand this :*D
Re^2: Loop and Add more cookies
by Accelerator (Initiate) on Oct 12, 2015 at 15:42 UTC

    thanks mate, i made a mistake there the code is

    $number = 1; $cookies = ""; while ( $cookies ne "COOKIES"x$number."S " x $number, ) { $number =++$number; print "Cookie monster: I want"." COOKIES"x$number."S" x $number, "! +\n"; chomp($cookies= <STDIN>); } print "Mmmm. $number COOKIES. :-)\n"; exit;

    but the task was to find an answer to end the script in the command prompt not to write a script, but i appreciate your help:)

      the task was to find an answer to end the script
      D'oh! In the original script, it was COOKIESCOOKIES, in the new one, it's COOKIESCOOKIESS S (including the trailing space). Your code still has some problems I addressed in my previous reply.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        thank you a lot, i am certain that you are very good at this. I didn't write this script, my teacher gave this script and told me to find the answer (what to type into the command prompt to end this script).