Array Question

Post your scripting questions / solutions here

Moderator: Moderators

Post Reply
digitac
Sergeant
Posts: 58
Joined: Mon Jul 28, 2003 3:30 pm

Array Question

Post by digitac »

Hi Scripting Geniussus :D

Here is my question

How can i create an array from a text like this

Text1 Text2 Text3 Text4

So what i want is to creat an array like this

$array
[0] => Text1
[1] => Text2
[2] => Text3
[3] => Text4


hope someone can help me with this

DigitaC
digitac
Sergeant
Posts: 58
Joined: Mon Jul 28, 2003 3:30 pm

Post by digitac »

Ok I got it i think

I have this now

Code: Select all

local.maparray = makeArray
	getcvar ("rcon g_maplist")
end array

for (local.n=1;local.n < local.maparray[1].size;local.n++)
{
	iprint local.maparray[local.n]
}
Now i hope someone can tell me what i am doing wrong
User avatar
tltrude
Chuck Norris
Posts: 4774
Joined: Sun Jul 07, 2002 4:03 am
Location: Oklahoma, USA
Contact:

iprint

Post by tltrude »

"iprint" will only print to the console and I think developer mode must be set. Try "iprintln" to print on the left side of the screen in yellow.
Tom Trude,

Image
nuggets
General
Posts: 1006
Joined: Fri Feb 28, 2003 2:57 am
Location: U-england-K (england in the UK) :P
Contact:

Post by nuggets »

you have
for (local.n=1;local.n < local.maparray[1].size;local.n++)
this'll not work as local.n will never be less than 1 (local.maparay[1].size)

so use
for (local.n=1;local.n<=local.maparray.size;local.n++)
hope this helps, prob not cos it's all foreign 2 me :-/
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

makeArray is often hard to use and I think you didn't use it correctly.

It's however way easier to do it this way:

Code: Select all

local.maparray[1] = getcvar g_maplist
local.maparray[2] = ....
local.maparray[3] = ....
Not sure why you typed 'getcvar (rcon g_maplist)'. Remember a script only runs on the server.
Image
digitac
Sergeant
Posts: 58
Joined: Mon Jul 28, 2003 3:30 pm

Post by digitac »

Yo Jv I get your point but what i really want is to put all the maps in the maplist to the array
zo that for example i have
obj/obj_team1 obj/obj_team2 obj/obj_team3 obj/obj_team4

local.maparray[1] = obj/obj_team1
etc..


greetz
DigitaC
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

Ah I see, so basically you don't want to create an array but you want to convert a string into an array :wink:

Not sure if makeArray can do this for you, otherwise you'll have to use a more straightforward way which is nonetheless pretty complicated as you need to parse a string.

I think something like this will work:

Code: Select all

main:
	local.string = getcvar g_maplist
	local.i = 0 // to be used below
	// scan the string for spaces, need to start at 0 since strings start indexing at 0
	local.startchar = 0
	for(local.charnum = 0; local.charnum < local.string.size; local.charnum++)
	{
		local.char = local.string[local.charnum] // gets the 'numth' character from the string
		if(local.char = " ")
		{
			// write the stuff from local.startchar to local.char to array at index i
			local.i++
			local.array[local.i] = waitthread writepartialstring local.string local.startchar local.char
			// set startchar to current char + 1
			local.startchar = local.charnum + 1
		}
	}
	
	/*
	Now you should have an array like this:
	local.array[1] = obj/obj_team1
	local.array[2] = obj/obj_team2
	....
	*/
end

writepartialstring local.string local.startchar local.endchar:
	local.outstring = "" // initialize a string 
	for(local.charnum = local.startchar; local.charnum < local.endchar; local.charnum++)
	{
		local.outstring += local.string[local.charnum]
	}
end local.outstring
I realize this is pretty complicated scripting, but you are trying to achieve a pretty complicated thing. I didn't actually test this so you might need to fiddle with it a bit. Good luck!
Image
Post Reply