SA-MP Forums Archive
Separating numbers in string and assinging them into variables. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Separating numbers in string and assinging them into variables. (/showthread.php?tid=366389)



Separating numbers in string and assinging them into variables. - LetsOWN[PL] - 06.08.2012

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



Re: Separating numbers in string and assinging them into variables. - LetsOWN[PL] - 06.08.2012

bump


Re : Separating numbers in string and assinging them into variables. - mamorunl - 08.09.2012

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

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