[Include] Dynamic String Splitter [Enumerator Functions Included!]
#7

I tried out the example you posted here, and it doesn't work the way you said it would.

Код:
public OnGameModeInit()
{
	new
		playerid,
		testid = 5,
		item_info[] = "1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20"
	;
	
	sscanf(item_info, "p<|>iiiiiiiiiiiiiiiiiiii", aInventory[playerid][0][inventory_item_id], aInventory[playerid][0][inventory_item_extra], aInventory[playerid][1][inventory_item_id], aInventory[playerid][1][inventory_item_extra], aInventory[playerid][2][inventory_item_id], aInventory[playerid][2][inventory_item_extra], aInventory[playerid][3][inventory_item_id], aInventory[playerid][3][inventory_item_extra], aInventory[playerid][4][inventory_item_id], aInventory[playerid][4][inventory_item_extra], aInventory[playerid][5][inventory_item_id], aInventory[playerid][5][inventory_item_extra], aInventory[playerid][6][inventory_item_id], aInventory[playerid][6][inventory_item_extra], aInventory[playerid][7][inventory_item_id], aInventory[playerid][7][inventory_item_extra], aInventory[playerid][8][inventory_item_id], aInventory[playerid][8][inventory_item_extra], aInventory[playerid][9][inventory_item_id], aInventory[playerid][9][inventory_item_extra]);
	printf("%i %i|%i\n", testid, aInventory[playerid][testid][inventory_item_id], aInventory[playerid][testid][inventory_item_extra]);

	new integers[MAX_INV_ITEMS], count[2];
	for(new i = 0, j = split_integers(item_info, "|", integers); i < j; i += sizeof(aInventory[][]))
	{
		for(new k = 0; k < sizeof(aInventory[][]); k ++)
		{
			aInventory[playerid][count[0]][eInventory:k] = integers[count[1] ++];
		}

		count[0] ++;
	}
	
	printf("%i %i|%i", testid, aInventory[playerid][testid][inventory_item_id], aInventory[playerid][testid][inventory_item_extra]);
	return 1;
}
Second one goes beyond the MAX_INV_ITEMS (10) array index.

I re-wrote the code, and it should work:
Код:
new integers[MAX_INV_ITEMS*2], count, limit = sizeof (aInventory[])/2;
for (new i, k, j = split_integers(item_info, "|", integers); i < j; i++)
{
    for (k = 0; k < sizeof (aInventory[][]); k++) // I removed "new" and added it to the main loop too, because it's better for optimization. (Creating stuff in loops = bad idea)
    {
		aInventory[playerid][count][eInventory: k] = integers[count+k];
    }
    count++;

    if (count == limit)
        break;
}
Anyway, good job!


EDIT:

I suggest optimizing the code a bit.
For example, instead of looping and replacing, you should take advantage of sscanf's amazing speed.

Код:
stock split_integers(string_to_split[], const separator[], integers[])
{
	new _format[20], count = 1;
	for(new i = 0, j = strlen(string_to_split); i <= j; i ++)
	{
		if(string_to_split[i] == separator[0])
		{
			string_to_split[i] = ' ';
			count ++;
		}
	}

	format(_format, sizeof(_format), "a<i>[%d]", count);

	sscanf(string_to_split, _format, integers);
	return count;
}
You should add an extra parameter called "size", with a default value of "sizeof (integers))"
Not only does it remove the need for looping, but it also limits error (If the array in sscanf is bigger than "integers[]" for example)

Код:
stock split_integers(string_to_split[], const separator[], integers[], size = sizeof (integers))
{
	new _format[21];
	format(_format, sizeof(_format), "p<%c>a<i>[%d]", separator[0], size);

	sscanf(string_to_split, _format, integers);
	return size;
}
But anyway, great job!
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)