in reply to Re: Loop and Add more cookies
in thread Loop and Add more cookies

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;

Replies are listed 'Best First'.
Re^3: Loop and Add more cookies
by choroba (Cardinal) on Oct 12, 2015 at 20:30 UTC
    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