PDA

View Full Version : How do I seperate Two|Values?


Darrenr
19/04/2015, 03:53 PM
Hi,

Ive done a bit of searching but cant find the right results..

I want to send two values to my NPC script using:

SendClientMessage(npcId, NPC_SetState, "5|6");

Is there then a way to seperate the 5 and 6 into two different variables in the NPC script? Im sure there is a simple way to do this, but i cant think of what it is.

Misiur
19/04/2015, 04:00 PM
If you are certain that format is always %d|%d, then you can do it manually (might return wonky values if format is different than %d|%d, or the first value has more than 63 characters):

split(const input[], &num1, &num2)
{
new
first[64],
delim = strfind(input, "|")
;

if (delim == -1) return -1;

strmid(first, input, 0, delim - 1);

num1 = strval(first);
num2 = strval(input[delim + 1]);

return 0;
}

Sscanf is neat too:
sscanf(input, "p<|>dd", num1, num2);

MouizGhouri
19/04/2015, 04:05 PM
Good Job
+Rep

SickAttack
19/04/2015, 04:09 PM
Here's another way (You can extract certain values that you want/need, you don't have to extract all of the values in the string just to use a specific value. This is just an example, if you want to remove the value extracted from the main string, use strdel with strfind):
new string[4], search[4], value_1[2], value_2[2];
format(string, sizeof(string), "5|6");

format(search, sizeof(search), "5");
strmid(value_1, string, strfind(string, search, true), strfind(string, search, true) + strlen(search));

format(search, sizeof(search), "6");
strmid(value_2, string, strfind(string, search, true), strfind(string, search, true) + strlen(search));

printf("Value 1: %s, Value 2: %s", value_1, value_2); // Debug.

Misiur
19/04/2015, 04:24 PM
@SickAttack:
format(search, sizeof(search), "5");

ಠ_ಠ
Please don't do that. There are other options available:
//1 (assuming no strcpy available)
search[0] = EOS;
strcat(search, "5");

//2
search[0] = 5, search[1] = EOS;

//3 - not much better one
valstr(search, 5);

SickAttack
19/04/2015, 04:38 PM
@SickAttack:
format(search, sizeof(search), "5");

ಠ_ಠ
Please don't do that. There are other options available:
//1 (assuming no strcpy available)
search[0] = EOS;
strcat(search, "5");

//2
search[0] = 5, search[1] = EOS;

//3 - not much better one
valstr(search, 5);

It's just an example, he can use it to search for different strings, somehow longer.

format(string, sizeof(string), "Admin Level 7|Admin Level 1337"); // Permissions.
format(search, sizeof(search), "Admin Level 1337");

But in this case, yeah, it's better to use one of the previous stated methods.

Misiur
19/04/2015, 04:45 PM
Close, but no dice. Thanks for remiding me to copy ******'s thead.

http://forum.sa-mp.com/showthread.php?p=3435601#post3435601 Check out "Copying strings"

Darrenr
19/04/2015, 08:31 PM
If you are certain that format is always %d|%d, then you can do it manually (might return wonky values if format is different than %d|%d, or the first value has more than 63 characters):

split(const input[], &num1, &num2)
{
new
first[64],
delim = strfind(input, "|")
;

if (delim == -1) return -1;

strmid(first, input, 0, delim - 1);

num1 = strval(first);
num2 = strval(input[delim + 1]);

return 0;
}

Sscanf is neat too:
sscanf(input, "p<|>dd", num1, num2);

This functions seems to be giving num1 as 0 and num2 as the correct value. Ive done a bit of debugging with it but cant seem to figure out why this is happening? The correct values ARE defiantly being passed throug. ive gotten it to work by using

num1 = strval(input[0]);
num2 = strval(input[2]);

as a temporary fix... and wen u print out all the values of the delim and that stuff, it all seems to add up? any ideas?

Misiur
19/04/2015, 08:52 PM
Ouch, sorry, it seems the "end" is not included, so just change delim -1:
strmid(first, input, 0, delim);

Strings are just arrays, with additional '\0'/EOS at the end:
static const input[] = { '5', '|', '6', EOS };
Most string functions, such as strval, strlen loop through characters until they find '\0'/EOS. So
strval(input[0]) is the same as strval(input), and strval(input[2]) will start from third character, '6' in our case.

If you know that, sorry for explaining.

Jefff
19/04/2015, 09:30 PM
Simply
split(const input[], &num1, &num2)
{
new pos = strfind(input, "|", true);

if(pos == -1) return -1;

num1 = strval(input);
num2 = strval(input[pos + 1]);

return 0;
}

Ahmad45123
19/04/2015, 09:37 PM
Just use sscanf.