http://www.perlmonks.org?node_id=1086385


in reply to Re: Program Not Printing Help
in thread Program Not Printing Help

Dave, Please explain the reason for the zero. It appears to force the scalar result to be a number rather than a string.
Bill

Replies are listed 'Best First'.
Re^3: Program Not Printing Help
by davido (Cardinal) on May 17, 2014 at 04:07 UTC

    That's a good question. Reverse treats its scalar operand as a string, which works fine most of the time, with one notable exception.

    My understanding of numerical palindromes is that 01 should be treated as a number such that its palindrome is 1. If I don't force the value to be cast as a number, reverse will return 10 if given 01 to start with. This may be appropriate for strings, but for numeric values the leading zero should be disregarded, as it is not significant.

    It's possible I'm wrong in my interpretation of how numbers with leading zeros should be treated when considering whether they are palindromes, but my interpretation makes sense to me in my possible ignorance. ;)

    By casting our input as a number before handing it to reverse, we cause the leading zeros to vanish before reverse has a chance to see them and do the wrong thing with them.


    Dave