IsNumeric Explanation
#1

I need to use this IsNumeric code in my script and it works, but I like to avoid using code created by other unless it's in the form of an include library, such as YSI and if I MUST borrow some code, I always like to understand exactly how it works.

Код:
for(new i = 0, j; (j = string[i]); i++)
{
     if (j < '0' || j > '9')
     {
          return 0;
     }
}
return 1;
I understand that it's comparing characters. The part I don't understand is the "(j = string[i])" part. I've seen a few of these IsNumeric functions with "j" being set to the string length and the loop ending once "i" is equal to "j", or the string length but I don't see how "(j = string[i])" tells the loop when to stop. It's setting "j" to each character in the string and checking if they are numeric, so if the string is numeric, does the loop simply stop when it reaches the end of the string then?

The other thing I don't get is why "(j = string[i])" needs brackets. If you remove the brackets it gives a warning saying that it may be an unintentional assignment statement. It is an assignment statement and you usually don't assign a value in that part of the loop so, it right in assuming that it's unintentional, but that's what's got me confused.

In short, I don't understand how assigning "j" the each character in the string tells the loop when to stop.
Reply
#2

Once you run into something that's not numeric loop stops. 123A34342 as string function will loop until it finds that A and then return 0 comes into the place.
Reply
#3

I dunno, but I've always used this:
pawn Код:
IsNumeric(const string[])
{
    for(new i = 0, j = strlen(string); i < j; i++)
    {
        if(string[i] > '9' || string[i] < '0') return 0;
    }
    return 1;
}
Reply
#4

it just loops each character in the string and checks if its a number between 0 and 9? whats so complicated?
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
The brackets are there to stop the warning, because you are right that you don't normally have an assignment in loops, but in this case you do. As for why it stops, strings are "NULL" terminated. That means that after all the characters you see there is a special character, which is empty. The loop keeps going while the current character is not empty.

Anyway, if you want to use YSI, and not custom functions (frankly a silly position but there we go), there is an "isnumeric" function in there already (a better one than both posted here btw):

pawn Код:
/**--------------------------------------------------------------------------**\
<summary>isnumeric</summary>
<param name="str[]">String to check</param>
<returns>
    -
</returns>
<remarks>
    Checks if a given string is numeric.
</remarks>
\**--------------------------------------------------------------------------**/


stock isnumeric(str[])
{
    P:7("isnumeric called: \"%s\"", str);
    new
        i;
    while (IS_IN_RANGE(str[i], '0', '9' + 1)) ++i;
    //while ((ch = str[i++])) if (!('0' <= ch <= '9')) return 0;
    return !str[i];
}
I never said I didn't want to use custom functions. I said I don't like taking random code that people post online. If someone posts code for something, I usually try to rewrite it better and differently so I understand it and so I'm not just taking peoples code.

So does the loop use the assignment statement like it would use "i < MAX_PLAYERS" and when it hits "NULL", it stops? I'm just confused as to how the assignment also acts as a comparison.

Quote:
Originally Posted by Pulsefire
Посмотреть сообщение
Once you run into something that's not numeric loop stops. 123A34342 as string function will loop until it finds that A and then return 0 comes into the place.
Like I clearly stated in the post, suppose the the string IS numeric. Why would it stop?

Quote:
Originally Posted by Ihateyou
Посмотреть сообщение
it just loops each character in the string and checks if its a number between 0 and 9? whats so complicated?
I didn't ask what it did. Your reply clearly shows how well you read.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)