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

Hello there, I'm pretty new and we started to learn to use perl, however i got a script and tried to find an answer for three days already and still couldn't figure it out. I tried several approaches but always kept failing. Hope you guys can enlighten me how to get it done :)

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

Replies are listed 'Best First'.
Re: Loop and Add more cookies
by choroba (Cardinal) on Oct 12, 2015 at 15:29 UTC
    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";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Loop and Add more cookies
by stevieb (Canon) on Oct 12, 2015 at 15:20 UTC

    It is kind of difficult to understand exactly what you want here. Could you please specify what your expected output should look like?

    Also, please put use warnings; and use strict; at the top of your script and attempt to fix your errors (always, always, always use these two lines. Even the top-most experienced Monks here always use them). If you don't understand the error output, a quick search here or on Google will explain things. If you're still confused as to what the error output means, by all means, reply back and we'll explain it fully to you.

    -stevieb

      We have to find an answer to end the loop, (basically we run the script and need to type in for example "2 Cookies" to end the loop if we fail the script loops and adds more, now i need to type in i.e. 3 Cookies, but i guess thats unnecessary to mention)

Re: Loop and Add more cookies
by Anonymous Monk on Oct 12, 2015 at 17:30 UTC
    Also remember that you must look at the actual HTTP that is being sent to the host and the actual (headers of the) HTTP response that is received. You can easily have cookie issues because, say, you think that the host received and stored the cookie but it did not ... whether due to a protocol problem or a typo or what-not on your part. "Trust, but verify." Never assume that you really know what is happening. You can spend many fruitless hours chasing white rabbits where you assumed that you knew what the error was, but did not.