Page 1 of 1
Array Question
Posted: Thu Jan 08, 2004 10:26 am
by digitac
Hi Scripting Geniussus
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
Posted: Thu Jan 08, 2004 10:43 am
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
iprint
Posted: Thu Jan 08, 2004 6:25 pm
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.
Posted: Thu Jan 08, 2004 11:07 pm
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++)
Posted: Fri Jan 09, 2004 8:34 am
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.
Posted: Fri Jan 09, 2004 10:00 am
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
Posted: Fri Jan 09, 2004 4:27 pm
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
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!