If comparison driving me up the wall...

paul_one
Right, I've looked up and down, left and right... Circular, back in time, and every which way I can... I've looked every which way - but loose, and I'm hoping you look that way at the following code (sorry - that movie just jumped into my head).

So this if is driving me crazy - it works everywhere else, but here...
		set numeric <CML_stotal; $parameter(1)$>
if NOT ( $parameter(1)$ = %CML_stotal% ) then {
debug <MATH ERROR == SUB ==>
debug <MATH ERROR == Library Parameter "$parameter(1)$" is not a number. Skipping...>
}

The if compares a numeric <$parameter(1)$> to a normal $parameter(1)$
Then if it's a number it will reply with that - if NOT, it'll print this debug message and then luckily, quest defaults the error'd numeric to 0...

BUT when I pass any number into there, I get the if activating... BUT even MORE weird is the fact the numeric and the parameter ARE the same... yet they aren't coming out the comparison the same... WHY!?!?
*rips out hair*
Anyone hazard a guess?

I think Im Dead
It's probably just quest being picky about the NOT aspect. I think it's a bit out of date and is mostly used for checking if an object has a property or action really.

Have you thought of just checking if %CML_stotal% <> $parameter(1)$ (or vice-versa)?

Seems like the obvious work around, though couldn't you just check the length of %CML_stotal% as a string put into a numeric would result in 0, or just checking if %CML_stotal% is equal to nothing? Both of those seem like easy enough ways to go around it, I'm thinking it is just a muck up with the NOT piece of code.

paul_one
I think I remember something like <> and it complaining... But I'll try again later.
I Just can't understand why it goes past that bit every time...
The reason I don't use zero - is because it is a valid number. And the $lengthof()$ function I think only works on strings... Numerics would result in 1 or more characters anyway - so either way that's impossible to tell.

::EDIT::
OK... Did what you suggested ITID, <> does work... I can't remember why I didn't use it in the first place - but I must have had a reason.
Anyway, thanks for the assistance!

Anonymous
Hi guys

Testing for a number not a string is curiously awkward to do in ASL, for example passing the string '007Bond' (where did I get that from I wonder) to a function will see it evaluated as 7 wheras 'Bond007' will be evaluated as 0. :shock:

So basically, if your function accidentally gets passed a variable that contains a mix of alpha & numerics, results are going to be somewhat unpredictable.

Although it is long winded, I think I'd go with something that tests the passed parameter for valid characters - that way if any non numerics are in the passed variable the whole thing is evaluated as being a string & returns 0. Something like:

define function <IsNumber>

set string <Test_For; 0123456789->
set string <Passed_Val; $parameter(1)$>
set numeric <Test_Result; $parameter(1)$>
set numeric <Passed_Len; $lengthof(#Passed_Val#)$>

if (%Passed_Len% = 0) then {
set <Test_result;0>
}
else {
for <i;1;%Passed_Len%> {
set string <Test_Var;$mid(#Passed_Val#;%i%;1)$>
if ($instr(#Test_For#;#Test_Var#)$ =0 ) then set <Test_Result;0>
}
}
return <%Test_Result%>

end define


Here I've done it so that a passed variable that contains any non numeric returns zero (which is what Quest delivers for strings that don't start with numerics!) I think I'd probably prefer the return of the -32767 (or whatever it is exactly) that Quest returns for an undefined numeric variable.

Al (MaDbRiT)

paul_one
Hmmm, well - my main reson for this - is that the programmer *should* see any error in output, see the debug info (as printed) and then see that the stuff he's passing the function is not a number - or at least, not a proper number (007Bond is one example)... I will have to set the number to zero though...

I think my other functions skip the parameter altogether - but I'm not sure...

Thanks for the info though Al - helpful as ever!

007bond
007bond is actually a good example. Just on that point, i've been trying to write some code for MPlayer that gets the user's input, then checks each character to see if it is a number (0-9). It works, but it works to well. It says "Invalid Character" even when there are only numbers in it!

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums