The oldies are the goodies



  • Today's snippet of Pure Win is brought to you by nagios. Specifically the check_snmp_* plugins (and maybe others).

    sub isnnum { # Return true if arg is not a number
      my $num = shift;
      if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
      return 1;
    }

    (For those who don't get regex, the if succeeds if the string passed matches digits with a single optional decimal point anywhere but the last character).



  • @ChZEROHag said:

    sub isnnum { # Return true if arg is not a number

    Took me looking at this line 3 or 4 times before i saw the method is: isNnum, presumably the extra N is for Not?  Because 2 more chars would be too much?  Thinking it was isnum the comment made my head spin because the return arg was just the opposite of what I'd expect :)



  • Why exactly is this a WTF? Maybe I don't get it, but they're checking to make sure it's a valid number, simple as that.



  • @gms8994 said:

    Why exactly is this a WTF? Maybe I don't get it, but they're checking to make sure it's a valid number, simple as that.
     

    You might want to actually read the post before replying, it's quite obvious what the WTF is.



  • I think TRWTF is that they didn't read the Perl FAQ about matching numbers.



  • @JamesKilton said:

    @gms8994 said:

    Why exactly is this a WTF? Maybe I don't get it, but they're checking to make sure it's a valid number, simple as that.
     

    You might want to actually read the post before replying, it's quite obvious what the WTF is.

     

     

    Humour us and explain it.

     

     



  • @ChZEROHag said:

    (For those who don't get regex, the if succeeds if the string passed matches digits with a single optional decimal point anywhere but the last character).

    Actually, the "if" condition also succeeds for all of the following, err, numbers:

    • 123.
    • 123.Michael
    • 123%"&%/)=

    ...anything, as long as it starts with a decimal digit.

    (I like how the ;} looks like it's smiling at me.)



  • @Michael Buschbeck said:

    @ChZEROHag said:
    (For those who don't get regex, the if succeeds if the string passed matches digits with a single optional decimal point anywhere but the last character).

    Actually, the "if" condition also succeeds for all of the following, err, numbers:

    • 123.
    • 123.Michael
    • 123%"&%/)=

    ...anything, as long as it starts with a decimal digit.

    (I like how the ;} looks like it's smiling at me.)

    Actually, it returns the matching part, try it:

    perl -e 'print "123.Michael" =~ /^(\d+\.?\d*)|(^\.\d+)$/'
    123.

    perl -e 'print "123%\"&%/)=" =~ /^(\d+\.?\d*)|(^\.\d+)$/'
    123

     



  • @gms8994 said:

    Actually, it returns the matching part, try it:

    perl -e 'print "123.Michael" =~ /^(\d+\.?\d*)|(^\.\d+)$/'
    123.

    perl -e 'print "123%\"&%/)=" =~ /^(\d+\.?\d*)|(^\.\d+)$/'
    123

     

    Which means that the if succeeds.



  • @gms8994 said:

    Actually, it returns the matching part

    Only in list context (which "print" provides). In scalar context (like in an "if" condition) it returns a boolean, or what passes in Perl for one, indicating the success of the match:

    [~] perl -le 'print scalar("123.Michael" =~ /^(\d+\.?\d*)|(^\.\d+)$/)'        
    1
    [~] perl -le 'print scalar("123%\"&%/)=" =~ /^(\d+\.?\d*)|(^\.\d+)$/)'
    1
    [~] perl -le 'print scalar("moo" =~ /^(\d+\.?\d*)|(^\.\d+)$/)'            
    

    [~] _



  • @Michael Buschbeck said:

    [~] _

    Oh no you didn't!



  • @bstorer said:

    @gms8994 said:

    Actually, it returns the matching part, try it:

    perl -e 'print "123.Michael" =~ /^(\d+\.?\d*)|(^\.\d+)$/'
    123.

    perl -e 'print "123%\"&%/)=" =~ /^(\d+\.?\d*)|(^\.\d+)$/'
    123

     

    Which means that the if succeeds.

    Even if the matching part is '0'?


  • @lolwtf said:

    @bstorer said:
    @gms8994 said:

    Actually, it returns the matching part, try it:

    perl -e 'print "123.Michael" =~ /^(\d+\.?\d*)|(^\.\d+)$/'
    123.

    perl -e 'print "123%\"&%/)=" =~ /^(\d+\.?\d*)|(^\.\d+)$/'
    123

    Which means that the if succeeds.

    Even if the matching part is '0'?
     

    zemm@bambi:~$ perl -e ' print "0" =~ /^(\d+\.?\d*)|(^\.\d+)$/ ? "win\n" : "fail\n";'
    win
    zemm@bambi:~$ perl -e ' print "0" =~ /^(\d+\.?\d*)|(^\.\d+)$/;'
    0zemm@bambi:~$

     



  • @Michael Buschbeck said:

    @ChZEROHag said:
    (For those who don't get regex, the if succeeds if the string passed matches digits with a single optional decimal point anywhere but the last character).

    Actually, the "if" condition also succeeds for all of the following, err, numbers:

    • 123.
    • 123.Michael
    • 123%"&%/)=

    ...anything, as long as it starts with a decimal digit.

    (I like how the ;} looks like it's smiling at me.)

     

     I don't think so. I don't see any alpha matching characters in there. the \d should only match digits, right? Am I missing something? 

     

    EDIT:

     I see it now. Grouping and ^$. Got it. Nevermind.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.