Separating numbers in string and assinging them into variables.
#1

Hey lads,
I gotta little problem in here.

I was wondering, how can I separate from string integers and assign them into variables.
For example, I've this table:
pawn Код:
new _Numbers[5];
And a string:
pawn Код:
//Numbers[128]:
"1.54.63.2.64.96"
All I wanna do is too loop through this string and get letters into table "_Numbers".
So it would like something like this:
pawn Код:
_Numbers[0] = 1;
_Numbers[1] = 54;
//and so on
I've already wroten a script to get these numbers:
pawn Код:
stock GetNumbersFromString(string[], array[])
{
    printf("'Get Numbers From String' being called");
    new _rcst[12];
    new arrays = 0;
    for(new i = 0; i < strlen(string)*2; i++)
    {
        if(string[i] != '.')
            format(_rcst, 12, "%s%c", _rcst, string[i]);
        else if(string[i] == '.')
        {
            array[arrays] = strval(_rcst);
            printf(_rcst);
            format(_rcst, 12, "\0");

        }
       
        if(string[i+1] == EOS)
            break;
    }

}
Okay, I've created 2 global variables (lets say 'at the top of the script)
pawn Код:
new intStr[64];
new iVar[4];
In main() Ive wroten:
pawn Код:
format(intSTr, 40, "6.35.64.1.5.6.4.3.123.4.70.");
GetNumbersFromString(intStr, iVar);
printf(iVar2]);
Okay, awesome, when I start my server,
pawn Код:
// remamber this:
            printf(_rcst);
It prints:
6
35
64
1
5

And so on.. So it works.
But:
pawn Код:
array[arrays] = strval(_rcst);
Is not doing it thing.. in this stock array == iVar and [arrays] are numbers from 0 to X (X = (in number) last repeat of loop).
When I use in main() for e.g. printf(iVar[3]); which should print 1 it prints nothing (same effect you can get with print("\n");

Anyone has any suggestions on how to solve this?

Thx.
Greetz,
LetsOWN
Reply
#2

bump
Reply
#3

Came by this topic while searching, but you are better off with sscanf.

sscanf("p<.>ddddd", num1, num2, num3, num4, num5);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)