Author Topic: Finding specific word within variable  (Read 1350 times)

Say I have the variable $list .
And this variable is equivalent to this:
$list = "playerone" SPC "playertwo" SPC "playerthree";

What I want to do is find only one of these strings... this is my attempt
Code: [Select]
if($list $= "playerone")
{
     echo("true");
     return;
}

I know for a fact that this code won't work the way it's supposed to, it's just what I came up with...
How do I make it so that it will single out the word "playerone" ? (in order for it to return 'true')

If it helps, the purpose of this code would be to check to see if someone is on the list.
I need it to be able to read each string in the variable to see if their name or ID is there.

for(%i = 0; %i < getWordCount($list); %i++)
{
     %word = getWord($list, %i);
     //Do stuff
}


If you're just checking for stuff on a list or adding/removing something, you could use these if you want:
Code: [Select]
function hasItemOnList(%list, %item)
{
if(getWordCount(%list) <= 0)
return false;

for(%i = 0; %i < getWordCount(%list); %i++)
{
%word = getWord(%list, %i);
if(%word $= %item)
return true;
}

return false;
}

function addItemToList(%list, %item)
{
if(hasItemOnList(%list, %item))
return %list;

%list = trim(%list SPC %item);
return %list;
}

function removeItemFromList(%list, %item)
{
for(%i = 0; %i < getWordCount(%list); %i++)
{
%word = getWord(%list, %i);
if(%word $= %item)
{
%list = removeWord(%list, %i);
return %list;
}
}

return %list;
}

Then you could do:
Code: [Select]
if(hasItemOnList($list, "playerone"))
{
//do stuff
}
Note: If you're checking for a player name this may not work if someone's name has more than 1 word
« Last Edit: August 20, 2016, 11:18:10 PM by Kyuande »

Thank you, very useful!

instead of calling a function like "getWordCount" every iteration do this

Code: [Select]
%cnt = getWordCount(%str);
for(%i=0;%i<%cnt;%i++)
{
    //stuff
}

this way is much more efficient as you only call the getWordCount function once instead of n times

For some reason the function 'removeWord' doesn't seem to be working for me...
I even tested it out and messed with it in-game and it won't erase any words. ???

For some reason the function 'removeWord' doesn't seem to be working for me...
I even tested it out and messed with it in-game and it won't erase any words. ???

Code: [Select]
%str = "a b c d";
%str = removeWord(%str,1);
echo(%str);

will echo "a c d"

Then can someone explain to me why the word won't erase in THIS code?
Code: [Select]
function serverCmdUnBlockPM(%client, %input)
{
%target = findClientByName(%input);
%list = %client.PMBlacklist;
for(%i = 0; %i < getWordCount(%list); %i++)
{
%word = getWord(%list, %i);
if(%word $= %target.bl_id)
{
%list = removeWord(%list, %i);
%client.chatMessage("ID "@%target.bl_id@" has been unblocked.");
return;
}
}
}
I even tried using the function to remove it in-game, why won't it work??? so confused

You're not saving the modified list. It should be:

%client.PMBlacklist = removeWord(%list, %i);

Or you could use the functions Kyuande posted and just do
Code: [Select]
function serverCmdUnBlockPM(%client, %input)
{
%target = findClientByName(%input);

if(hasItemOnList(%client.PMBlacklist, %target.getBLID()))
{
%client.PMBlacklist = removeItemFromList(%client.PMBlacklist, %target.getBLID());
%client.chatMessage("ID" SPC %target.getBLID() SPC "has been unblocked.");
}
}

Also.
You should always use .getBLID() instead of .bl_id because it's more secure.
You can also use SPC instead of @ as a connector. It's the same thing except it adds a space. There's also TAB that connects it but adds a tab and NL which connects it and adds a new line.

You only need to do SPC/TAB/NL when you really need to

Things like: "Hello my name is" SPC %client.getPlayerName() is unneeded (you can still do this, to me this looks weird), but "Hello my name is " @ %client.getPlayerName() is better practice; unless someone tell me a reason why my first example is better?
Making a list like this: %list SPC %word1 SPC %word2 or %list TAB %word1 TAB %word2 is needed, such as making a vector or a normal list

Why do you think it's better practice? Because it uses a couple more characters?
I prefer to use SPC instead of @ in every situation that I can.

Well, some languages you only have + or whatever it is to combine strings

You only need to do SPC/TAB/NL when you really need to

Things like: "Hello my name is" SPC %client.getPlayerName() is unneeded (you can still do this, to me this looks weird), but "Hello my name is " @ %client.getPlayerName() is better practice; unless someone tell me a reason why my first example is better?
Making a list like this: %list SPC %word1 SPC %word2 or %list TAB %word1 TAB %word2 is needed, such as making a vector or a normal list

LOL not this again. I swear this comes up every month haha. It's the user's preference, that's it. I prefer using @ like you Kyuande (you SPCers are weird (jes)!!!)

Someone should run a benchmark, use @ and SPC 10 million times and see if it's really just preference :D

Now of course, if you're doing something like...
%name SPC %lastname
You can't necessarily just throw the space into the string.
Ignoring that though and straight to the tests. 10 million times. 3 tests. Compared using getRealTime.

The code:
Code: [Select]
%start = getRealTime();
for(%x=0;%x<10000000;%x++)
{
%str = "bob " @ "borker";
}
%end = getRealTime();
commandToClient(%client,'ServerMessage', '', '\c3Space @: %1', %end-%start);
%start = getRealTime();
for(%x=0;%x<10000000;%x++)
{
%str = "bob" @ " " @ "borker";
}
%end = getRealTime();
commandToClient(%client,'ServerMessage', '', '\c3Double @: %1', %end-%start);
%start = getRealTime();
for(%x=0;%x<10000000;%x++)
{
%str = "bob" SPC "borker";
}
%end = getRealTime();
commandToClient(%client,'ServerMessage', '', '\c3SPC: %1', %end-%start);
The results:
Space @ ("string " @ "string"): 2668
Double @ ("string" @ " " @ "string"): 3042
SPC ("string" SPC "string"): 2778

SPC is slower, but of course, if you're joining two vars by a space, it's better than doing @ " " @.