Script Array Upgrade
by Daniel Neilsen · 10/17/2003 (11:29 am) · 80 comments
Download Code File
This is a powerful array class which adds php style arrays to TGE script.
This means you can do the following:
- array pointers
This allows you to move forwards or backwards through the array
as if it was a list including jumping to the start or end
- sorting
You can sort in either alpha or numeric mode on the key or the
value in ascending or descending order
- adding and removing of elements from any array position
You can pop and push elements from the start and end of the
array and also insert or erase elements in the middle of the
array.
- removal of duplicates
You can remove duplicate keys or duplicate values.
- searching
You can search the array and return the index of a particular
key or value value
- Advanced Features
Several other advanced features like array appends, array
cropping and array duplication.
- Counting
Count the number of instaces of a particular value or key in
the array, as well as the number of elements
This array class will also support both string and number values.
INSTALLATION:
1) Backup your stuff
2) BACK IT UP!
3) Copy the arrayObject.cc file into the /engine/console dir
4) Compile
I have tested it with the latest head (release 1.2) but I am pretty sure it would work for all versions of TGE.
USAGE:
In script you can do the following
%myarray = new array();
%myarray.add(%key, %value);
%myarray.add("bla", 5.34);
%myarray.add("bla", "test");
COMMANDS:
add() - (string key, string value)Adds a new element to the end of an array
append() - (Array target)Appends the target array to the array object
count() - Get the number of elements in the array
countKey() - (string key)Get the number of times a particular key is found in the array
countValue() - (string value)Get the number of times a particular value is found in the array
crop() - (Array target)Removes elements with matching keys from array
duplicate() - (Array target)Alters array into an exact duplicate of the target array
echo() - Echos the array in the console
empty() - Emptys all elements from an array
erase() - (int index)Removes an element at a specific position from the array
getCurrent() - Gets the current pointer index
getIndexFromKey() - (string key)Search array from current position for the first matching key
getIndexFromValue() - (string value)Search array from current position for the first matching value
getKey() - (int index)Get the key of the array element at the submitted index
getValue() - (int index)Get the value of the array element at the submitted index
insert() - (string key, string value, int index)Adds a new element to a specified position in the array
moveFirst() - Moves array pointer to start of array
moveLast() - Moves array pointer to end of array
moveNext() - Moves array pointer to next position (returns -1 if cannot move)
movePrev() - Moves array pointer to prev position (returns -1 if cannot move)
pop_back() - Removes the last element from the array
pop_front() - Removes the first element from the array
push_back() - (string key, string value)Adds a new element to the end of an array
push_front() - (string key, string value)Adds a new element to the front of an array
sort() - (bool desc)Sorts the array by value (default ascending sort)
sorta() - Alpha sorts the array by value in ascending order
sortd() - Alpha sorts the array by value in descending order
sortkd() - Alpha sorts the array by key in descending order
sortka() - (bool desc)Sorts the array by key (default ascending sort)
sortn() - (bool desc)Sorts the array numerically by value (default ascending sort)
sortna() - Numerical sorts the array by value in ascending order
sortnd() - Numerical sorts the array by value in descending order
sortnka() - Numerical sorts the array by key in ascending order
sortnkd() - Numerical sorts the array by key in descending order
uniqueKey() - Removes any elements that have duplicated keys (leaving the first instance)
uniqueValue() - Removes any elements that have duplicated values (leaving the first instance)
Thats about it.
Any questions about usage or whatever, just post em here
Edit (28/10/03):
Added setValue and setKey methods - was a bit of an oversight to have missed them ;)
This is a powerful array class which adds php style arrays to TGE script.
This means you can do the following:
- array pointers
This allows you to move forwards or backwards through the array
as if it was a list including jumping to the start or end
- sorting
You can sort in either alpha or numeric mode on the key or the
value in ascending or descending order
- adding and removing of elements from any array position
You can pop and push elements from the start and end of the
array and also insert or erase elements in the middle of the
array.
- removal of duplicates
You can remove duplicate keys or duplicate values.
- searching
You can search the array and return the index of a particular
key or value value
- Advanced Features
Several other advanced features like array appends, array
cropping and array duplication.
- Counting
Count the number of instaces of a particular value or key in
the array, as well as the number of elements
This array class will also support both string and number values.
INSTALLATION:
1) Backup your stuff
2) BACK IT UP!
3) Copy the arrayObject.cc file into the /engine/console dir
4) Compile
I have tested it with the latest head (release 1.2) but I am pretty sure it would work for all versions of TGE.
USAGE:
In script you can do the following
%myarray = new array();
%myarray.add(%key, %value);
%myarray.add("bla", 5.34);
%myarray.add("bla", "test");
COMMANDS:
add() - (string key, string value)Adds a new element to the end of an array
append() - (Array target)Appends the target array to the array object
count() - Get the number of elements in the array
countKey() - (string key)Get the number of times a particular key is found in the array
countValue() - (string value)Get the number of times a particular value is found in the array
crop() - (Array target)Removes elements with matching keys from array
duplicate() - (Array target)Alters array into an exact duplicate of the target array
echo() - Echos the array in the console
empty() - Emptys all elements from an array
erase() - (int index)Removes an element at a specific position from the array
getCurrent() - Gets the current pointer index
getIndexFromKey() - (string key)Search array from current position for the first matching key
getIndexFromValue() - (string value)Search array from current position for the first matching value
getKey() - (int index)Get the key of the array element at the submitted index
getValue() - (int index)Get the value of the array element at the submitted index
insert() - (string key, string value, int index)Adds a new element to a specified position in the array
moveFirst() - Moves array pointer to start of array
moveLast() - Moves array pointer to end of array
moveNext() - Moves array pointer to next position (returns -1 if cannot move)
movePrev() - Moves array pointer to prev position (returns -1 if cannot move)
pop_back() - Removes the last element from the array
pop_front() - Removes the first element from the array
push_back() - (string key, string value)Adds a new element to the end of an array
push_front() - (string key, string value)Adds a new element to the front of an array
sort() - (bool desc)Sorts the array by value (default ascending sort)
sorta() - Alpha sorts the array by value in ascending order
sortd() - Alpha sorts the array by value in descending order
sortkd() - Alpha sorts the array by key in descending order
sortka() - (bool desc)Sorts the array by key (default ascending sort)
sortn() - (bool desc)Sorts the array numerically by value (default ascending sort)
sortna() - Numerical sorts the array by value in ascending order
sortnd() - Numerical sorts the array by value in descending order
sortnka() - Numerical sorts the array by key in ascending order
sortnkd() - Numerical sorts the array by key in descending order
uniqueKey() - Removes any elements that have duplicated keys (leaving the first instance)
uniqueValue() - Removes any elements that have duplicated values (leaving the first instance)
Thats about it.
Any questions about usage or whatever, just post em here
Edit (28/10/03):
Added setValue and setKey methods - was a bit of an oversight to have missed them ;)
About the author
#2
10/14/2003 (7:52 pm)
Nice job... This will make programming in script a lot less painful. Just the sort of data structure we need to make TScript really useful! :)
#3
10/17/2003 (3:09 pm)
W00T :) Thanks dan, This was waaaay overdue as a scripting feature...
#4
10/17/2003 (8:53 pm)
Good Lord... =)~
#5
10/18/2003 (11:49 am)
Oh hell ya! Someone put this sucker in the next HEAD! :-) It's got a lot of functionality I want :-)
#6
10/19/2003 (6:24 am)
nice:)
#7
10/22/2003 (9:08 am)
This is AWESOME. Now what would be REALLY freakin great, is if you could modify the scan/gram lex/yacc files to turn around and actually implement the [] operator for these objects.
#8
10/22/2003 (11:02 am)
excellente! We all appreciate it!
#9
10/22/2003 (4:22 pm)
Very, very, very nice Daniel!
#10
I guess its not that much of a problem for what I'm using it for, but figured its worth pointing out. Nice work, anyways ;-)
10/27/2003 (8:23 am)
Call me stupid, but I cant see any way in the current code to change a value once its in the array, so you have to remove and re-add it if you want to change the value. This just seems silly :)I guess its not that much of a problem for what I'm using it for, but figured its worth pointing out. Nice work, anyways ;-)
#11
The class has been updated with setKey and setValue methods
10/28/2003 (3:35 am)
My bad, I clean forgot about them.The class has been updated with setKey and setValue methods
#12
Example:
$a = new array();
$a.add(1,"testing");
echo($a.getValue(2));
%@
I must agree though. Very nice class. And some changes to the flex parsing will make it karma ++!
11/03/2003 (3:05 am)
Though this is a nice class, it can do with some more sanity checking.Example:
$a = new array();
$a.add(1,"testing");
echo($a.getValue(2));
%@
I must agree though. Very nice class. And some changes to the flex parsing will make it karma ++!
#13
11/03/2003 (5:35 am)
Sweet!
#14
02/06/2004 (2:28 am)
Shouldn't this go into HEAD? ;) *hint, hint*
#15
The console methods for setKey and setValue
Here is the getIndexFromValue method.
Both getIndexFromKey and getIndexFromKeyValue look very similar and intend to return "-1" if the string isn't found.
Only problem here is we are using a U32 to store foundIndex (and as the return value), so the entire block for if(foundIndex < 0) will never run because -1 is translated to its unsigned bitwise equivalent.
Cory
04/19/2004 (9:53 pm)
Decent resource, however I've found a couple of bugs :(. Its surprising these haven't popped up anywhere with the age of the comments above, but I just happened to notice these as I spent time debugging what I thought were errors in my scripts.The console methods for setKey and setValue
... StringTableEntry key = StringTable->insert(argv[2]); U32 index = dAtoi(argv[2]); ...are reading in argv[2] for both the key and the index. Index should be reading argv[3].
Here is the getIndexFromValue method.
U32 Array::getIndexFromValue( StringTableEntry value)
{
U32 foundIndex = -1;
for(U32 i=mCurrentIndex; i<mArray.size(); i++)
{
if(mArray[i].value == value)
{
foundIndex = i;
break;
}
}
[b] if(foundIndex < 0) [/b]
{
for(U32 i=0; i<mCurrentIndex; i++)
{
if(mArray[i].value == value)
{
foundIndex = i;
break;
}
}
}
return foundIndex;
}Both getIndexFromKey and getIndexFromKeyValue look very similar and intend to return "-1" if the string isn't found.
Only problem here is we are using a U32 to store foundIndex (and as the return value), so the entire block for if(foundIndex < 0) will never run because -1 is translated to its unsigned bitwise equivalent.
Cory
#16
Now, shouldn't this go into HEAD finally? :-) (Trying slightly louder than Stepan: *HINT HINT!*)
04/22/2004 (8:37 pm)
Nice fix, Cory :-)Now, shouldn't this go into HEAD finally? :-) (Trying slightly louder than Stepan: *HINT HINT!*)
#17
07/17/2004 (12:51 pm)
Did this go into head?
#18
the %idleTask.task field will not print out(!?)
But if I remove the "subtasks = new Array();" line, then it prints out fine. Is this a problem with the script engine or something weird about the Array class?
Update: it also works ok if I reverse the order of those two fields. Weird.
03/17/2005 (6:08 pm)
I'm seeing a very weird problem and I'm not sure if it's related to this class or not. But if I have code like this:%idleTask = new SimObject()
{
task = "Idle";
subtasks = new Array();
};
echo("task id:" SPC %idleTask SPC "task:" SPC %idleTask.task);the %idleTask.task field will not print out(!?)
But if I remove the "subtasks = new Array();" line, then it prints out fine. Is this a problem with the script engine or something weird about the Array class?
Update: it also works ok if I reverse the order of those two fields. Weird.
#19
04/23/2005 (3:25 pm)
Someone should perhaps update the actual file with the above bugs fixed. And also separate the files into .h and .cc files. This is really something I found useful, to take some of the clunkyness out of TScripting.
#20
Currently one of the methods in the code reads as:
I believe the one line there should read:
Array updates weren't working for me without this change.
05/03/2005 (4:08 pm)
Nice resource. Flipped through it earlier because another resource was using this.Currently one of the methods in the code reads as:
ConsoleMethod( Array, setValue, void, 4, 4, "(string key, int index)"
"Set the value at the given index")
{
StringTableEntry value = StringTable->insert(argv[2]);
U32 index = dAtoi(argv[2]);
object->setValue(value, index);
}I believe the one line there should read:
U32 index = dAtoi(argv[3]);
Array updates weren't working for me without this change.

Torque Owner Christian M Weber