SPUG: number inside of a number

jerry gay jerry.gay at gmail.com
Tue Sep 19 21:47:47 PDT 2006


On 9/19/06, jerry gay <jerry.gay at gmail.com> wrote:
> if( '5' eq chop $number ) { ... }
>
sorry, i hit 'send' too soon. this hack deserves explanation. as it
compares strings, it doesn't suffer from the "what if it's not a
number"  problem, but *WARNING* it is destructive. 'chop' *removes*
the last character from $number and returns it, which is probably not
what you expect. so,

if( '5' eq substr $number, -1 ) { ... } # using string comparison
or
if( 5 == $number % 10 ) { ... } # using numeric comparison

both of which were previously mentioned, are better ways to do it.
the % hack is neat, but i think i'd stick with substr, which will work
properly if $number doesn't contain a number.
~jerry


More information about the spug-list mailing list