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

Hello Monks,
I am working on a little CGI App. And I am having some problems with the following code.

$p_challange = $challange . ":" . time; $sys->p_log("Challange: $p_challange\n"); $sys->p_log("String Lenght: " . lenght($p_challange) . "\n"); while ( lenght($p_challange) gt 16 || (lenght($p_challange) % 16) eq 0 +){ $p_challange .= "A"; }

the $sys->p_log function works and there is nothing wrong with it I am 100% sure for that.. But yea when I look in my Log File I get this:

Tue Nov 24 10:44:30 2009 Challange: X[[:yFq:1259055870
Anywho, well it dies on lenght. I have had that issue before so I am wondering.. is Lenght Decapitated or what's up with it? And/or is there another way to ask for the lenght of a string?

Thanks,

Robin

Replies are listed 'Best First'.
Re: lenght ()Decapitated?
by Corion (Patriarch) on Nov 24, 2009 at 10:00 UTC

    There are some issues with your code:

    First of all, the function is named length, not lenght (and it took me a loong time to learn that length and width are written that way and not lenght and widht...)

    Then, you're using gt to compare numbers. While that's technically OK, it will give you surprising results if you expect 1000 to be larger than 16.

    If your code "dies", it's always instructive to tell us the error message.

      Another surprise is if you expect '2' not to be greater than '16'.

      Thanks for mocking my Dyslexia.. XD
      It even took me a while to see the difference when you typed them next from each other.. lol
      And I used > before but I heard that its better to use gt but Ill change it back ^^

      Oh and the dies thing.. Well as its CGI I dont see the error.. ;)
      Thanks

        So first run your program from the command line, to see the errors. Then, later on, run it on the web host as CGI with CGI::Carp qw(fatalsToBrowser), and take a look into the webserver error log.

        I heard that its better to use gt

        Just to clarify: use gt when comparing text, use > when comparing numbers.

        Dyslexia should not enter into it.
        When showing code, copy and paste it into your code tags, rather than retyping it.

        Everybody makes typoes when banging out code, but with a copy/paste, it is a lot harder to hide the original problem under new bugs.

        We're not mocking your dyslexia, we're identifying the problem you've asked us to identify. If you were to check your web server's error log, you'd see your program is dying with the error

        Undefined subroutine &main::lenght called at ...
        You seem to have forgotten the context of that lesson.
        $ perl -le' print "gt w str: ", "b" gt "a" ? "ok : "XXX"; print "> w str: ", "b" > "a" ? "ok : "XXX"; print "gt w num: ", 16 gt 2 ? "ok : "XXX"; print "> w num: ", 16 > 2 ? "ok : "XXX"; ' gt w str: ok > w str: XXX b comes after a, but ">" said otherwise gt w num: XXX 16 comes after 2, but "gt" said otherwise > w num: ok
Re: lenght ()Decapitated?
by scorpio17 (Canon) on Nov 24, 2009 at 18:25 UTC

    As long as you're correcting typos - you spelled 'challenge' wrong, also.

    But as for your code: if the string length is greater than 16, or some multiple of 16 (i.e., 32, 48, 64...), then you want to append an 'A' to the string?! If the string is 16 or greater, this will become an infinite loop, since the length will always be greater than 16, no matter how many A's you add... so I'd suggest you rethink your logic.

      They thanks all.

      My latest comments on this.
      - First and most important, Thanks for the help
      - The Logic Error was indeed a mistake it meant to be something else but its fixed now.
      - D'oh for the Challenge thing..
      - The Webserver error log didn't say a thing. I think this has to do with the fact that its lighttpd.
      - Thanks all again.

      Cheers,
      Robin