Array initialization from string with SSCANF
#1

I want to create an array from a string but i dont know how im going to do that with sscanf.
https://sampforum.blast.hk/showthread.php?tid=570927 There is no example for my needs.

[code]
enum myenum{myid,myval};
new myarray[10000][800][myenum];

new myarraystring1[500]="1:12,3:32,12:27,2:39";
new myarraystring2[500]="7:4";
new myarraystring3[500]="4:4,76:12";

sscanf(
an_unknown_string,
"WHAT SHOULD I USE?",
myarray[3]); //3 is an example, it can be a number between 0-10000
[code]

for myarraystring1 result should be;
myarray[3][0][myid] = 1; myarray[3][0][myval] = 12;
myarray[3][0][myid] = 3; myarray[3][0][myval] = 32;
myarray[3][0][myid] = 12; myarray[3][0][myval] = 27;
myarray[3][0][myid] = 2; myarray[3][0][myval] = 39;
for myarraystring2 result should be;
myarray[3][0][myid] = 7; myarray[3][0][myval] = 4;
for myarraystring1 result should be;
myarray[3][0][myid] = 4; myarray[3][0][myval] = 4;
myarray[3][0][myid] = 76; myarray[3][0][myval] = 12;


an_unknown_string can be myarraystring1 or myarraystring2 or myarraystring3.
i dont know the size, i only know the limit and it is 800
that means string can include 800 different x:y's which seperated with commas.

what is the right expression to initialize myarray.
Reply
#2

For this specific array, you can do:
pawn Код:
enum myenum{myid,myval};
new myarray[10000][myenum];
new myarray_index;

public OnGameModeInit()
{
    new myarraystring1[]="1:12,3:32,12:27,2:39";
    new myarraystring2[]="7:4";
    new myarraystring3[]="4:4,76:12";
   
    Split_myarray(myarraystring1);
    Split_myarray(myarraystring2);
    Split_myarray(myarraystring3);
    return 1;
}
pawn Код:
Split_myarray(myarray_string[])
{
    new count, tmp_myarray_index = myarray_index;

    for (new i, j = strlen(myarray_string); i != j; i++)
    {
        if (myarray_string[i] == ',') count++;
    }
    if (count) tmp_myarray_index += ++count;
    else tmp_myarray_index++;
   
    for (new i = myarray_index; i != tmp_myarray_index; i++)
    {
        sscanf(myarray_string, "e<P<:,>ii>", myarray[i]);
        if (count) strdel(myarray_string, 0, strfind(myarray_string, ",") + 1);
    }
   
    myarray_index = tmp_myarray_index;
}
Reply
#3

You could also do this hack
PHP код:
sscanf(myarraystring1"P<:,>a<i>[1600]"myarray[0][0]);
sscanf(myarraystring2"P<:,>a<i>[1600]"myarray[1][0]);
sscanf(myarraystring3"P<:,>a<i>[1600]"myarray[2][0]); 
The 1600 would be 800 (2. dimension) * 2 (enum size)

But that only works for integer only enums
Reply
#4

Thank you so so much. I get it now. After couple of tryings i changed the type of string because of performance issues. Now i save data as 1 2 14 5 123 5 213 123. (2 by 2) And im using this to split them. "a<i>[10]"
after that i set the first val to myid and second one to myval in a one easy for loop. In that way i believe its more faster that previous ones.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)