Split: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
Line 16: Line 16:


==Example==
==Example==
This examples takes any console text input and splits it into parts.
<section name="Server" class="server" show="true">
This example gives the specified weapons to the given player, while the weapons are a string in the form: 'weaponId,ammo;weaponId2,ammo2;weaponId3,ammo3;..'. This is especially for data read from a .map file attribute.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onConsole ( text )
function giveWeapons(player, weaponsString)
    -- Grab the table of tokens
local weaponsTable = split(weaponsString, 59) --split the string by the semi colon
    splittext = split ( text, 32 )
for k,v in ipairs(weaponsTable) do --for all the split values do
    -- for each token there...
weaponId = gettok(v, 1, 44) --get the weapon ID using gettok, retrieve the first token
    for splitKey, splitVal in ipairs(splittext) do
weaponAmmo = gettok(v, 2, 44) --get the ammo using gettok, retrieve the second token
        -- output the index and token
if (weaponId ~= nil and weaponAmmo ~= nil) then --if neither of them is invalid
        outputChatBox ( splitKey .. ": " .. splitVal )
giveWeapon(player, weaponId, weaponAmmo) --give the player the weapons
    end
end
end
end
end
addEventHandler ( "onConsole", getRootElement ( ), onConsole )
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Utility functions}}
{{Utility functions}}

Revision as of 15:29, 29 August 2007

This function splits a string into substrings. You specify a character that will act as a separating character; this will determine where to split the sub-strings. For example, it can split the string "Hello World" into two strings containing the two words, by spliting using a space as a separator.

Note: You can use the function gettok to retrieve a single token from the string at a specific index. This may be faster for one-off lookups, but considerably slower if you are going to check each token in a long string.

Syntax

table split ( string stringToSplit, int separatingChar )

Required Arguments

  • stringToSplit The string you wish to split into parts.
  • separatingChar The character you want to use to split (ASCII number)

Returns

Returns a table of the substrings.

Example

Click to collapse [-]
Server

This example gives the specified weapons to the given player, while the weapons are a string in the form: 'weaponId,ammo;weaponId2,ammo2;weaponId3,ammo3;..'. This is especially for data read from a .map file attribute.

function giveWeapons(player, weaponsString)
	local weaponsTable = split(weaponsString, 59) --split the string by the semi colon
	for k,v in ipairs(weaponsTable) do --for all the split values do
		weaponId = gettok(v, 1, 44) --get the weapon ID using gettok, retrieve the first token
		weaponAmmo = gettok(v, 2, 44) --get the ammo using gettok, retrieve the second token
		if (weaponId ~= nil and weaponAmmo ~= nil) then --if neither of them is invalid
			giveWeapon(player, weaponId, weaponAmmo) --give the player the weapons
		end
	end
end

See Also