Here are a whole heap of array functions I created (should work for strings I think). They all come with examples and usage instructions. You can turn this into a GSC and use #include aswell. Before I was about to release this I realised that common_scripts/utility had some of these functions already.
Note that these functions were inspired by the IList Interface and Array Class from the .NET Framework.
PHP Code:
/*
An example of a bad predicate which returns no value at all and doesn't accept a value.
Arguments:
None.
*/
bad_predicate()
{
}
/*
An example of a 'predicate' which takes an object and returns
a boolean value.
Arguments:
obj - An object that is passed from an array.
*/
some_predicate(obj)
{
return obj == 1;
}
/*
Returns a boolean value which determines whether each value in an array matched the
criteria of the predicate.
Arguments:
arr - An array passed through as an argument.
predicate - A function pointer where the function returns a boolean value.
Example:
some_function()
{
some_array = [];
some_array[0] = 1;
some_array[1] = 1;
// This would display 1 on the screen (true)
self iPrintLn(_array_TrueForAll(some_array, ::test_predicate));
some_array[1] = 0;
// This would display 0 on the screen (false)
self iPrintLn(_array_TrueForAll(some_array, ::test_predicate));
}
test_predicate(obj)
{
return obj == 1;
}
*/
_array_TrueForAll(arr, predicate)
{
if(isArray(arr) && arr.size > 0 && valid_predicate(predicate, arr[0]))
for(i = 0; i < arr.size; i++)
if(![[predicate]](arr[i]))
return false;
return true;
}
/*
Returns a boolean value which checks if the predicate actually returns something.
Arguments:
predicate - A function pointer which returns a boolean value.
obj - An object that is passed from an array.
Example:
None, used internally by other functions.
*/
valid_predicate(predicate, obj)
{
return [[predicate]](obj) != undefined;
}
/*
Returns an array that has been reversed.
Arguments:
arr - An array passed through as an argument.
Example:
some_function()
{
my_array = [];
my_array[0] = "T";
my_array[1] = "E";
my_array[2] = "S";
my_array[3] = "T";
for(i = 0; i < 4; i++)
self iPrintLn(i + " : " + my_array[i]);
// Output from above:
// 0 : T
// 1 : E
// 2 : S
// 3 : T
// Note that you need to assign the array to the value
// returned by the function.
my_array = _array_Reverse(my_array);
for(i = 0; i < 4; i++)
self iPrintLn(i + " : " + my_array[i]);
// Output from above:
// 0 : T
// 1 : S
// 2 : E
// 3 : T
}
*/
_array_Reverse(arr)
{
ret_arr = arr;
if(isArray(arr))
{
for(i = arr.size - 1; i >= 0; i--)
ret_arr[arr.size-1-i] = arr[i];
}
return ret_arr;
}
/*
Returns an array that has a specific item removed using an index value (first occurence).
Arguments:
arr - An array passed through as an argument.
index - The array index where the item is to be removed.
Example:
some_function()
{
my_array = [];
my_array[0] = "removeMe";
my_array[1] = "leaveMeAlone";
// This would display 'The array currently has 2 item(s).'
self iPrintLn("The array currently has " + my_array.size + " item(s).");
my_array = _array_RemoveAt(my_array, 0);
// This would display 'The array now has 1 item(s).'
self iPrintLn("The array now has " + my_array.size + " item(s).");
}
*/
_array_RemoveAt(arr, index)
{
if(isArray(arr) && isDefined(index) && index != -1)
{
for(i = index; i < arr.size - 1 && i >= 0; i++)
arr[i+1] = arr[i];
arr[arr.size-1] delete();
}
return arr;
}
/*
Returns an array that has a specific item removed using an object (first occurence).
Arguments:
arr - An array passed through as an argument.
obj - An object that is used to remove a matching instance.
Example:
some_function()
{
this_array = [];
this_array[0] = 22;
this_array[1] = 33;
this_array[2] = 66;
// This would display 'The array currently has 3 item(s).'
self iPrintLn("The array currently has " + this_array.size + " item(s).");
this_array = _array_Remove(this_array, 33);
// This would display 'The array now has 2 item(s).'
self iPrintLn("The array now has " + this_array.size + " item(s).");
}
*/
_array_Remove(arr, obj)
{
return _array_RemoveAt(arr, _array_IndexOf(arr, obj));
}
/*
Returns an array that has a new item added to it.
Arguments:
arr - An array passed through as an argument.
obj - An object that is to be added to the end of the array.
Example:
some_function()
{
thing_array = [];
// This would display 'The array currently has 0 item(s).'
self iPrintLn("The array currently has " + thing_array.size + " item(s).");
thing_array = _array_Add(thing_array, "lookAtMe");
// This would display 'The array now has 1 item(s).'
self iPrintLn("The array now has " + thing_array.size + " item(s).");
}
*/
_array_Add(arr, obj)
{
if(isArray(arr) && isDefined(obj))
arr[arr.size] = obj;
return arr;
}
/*
Returns a boolean value determining if an object exists in an array.
Arguments:
arr - An array passed through as an argument.
obj - An object to look for.
Example:
some_function()
{
an_array = [];
an_array[0] = "findMe";
an_array[1] = "goAway";
// _array_Contains will return true and 'an_array contains 'findMe'' will be displayed
if(_array_Contains(an_array, "findMe"))
self iPrintLnBold("an_array contains 'findMe'");
else
self iPrintLnBold("an_array does not contain 'findMe'");
}
*/
_array_Contains(arr, obj)
{
return _array_IndexOf(arr, obj) != -1;
}
/*
Returns an integer value determining at which index of an array an object is (first occurence).
If it fails to find the object, it will return -1.
Arguments:
arr - An array passed through as an argument.
obj - An object to look for.
Example:
some_function()
{
me_array = [];
me_array[0] = "a";
me_array[1] = "b";
me_array[2] = "c";
// This would display 'The index of the value a is at: 0'
self iPrintLn("The index of the value a is at: " + _array_IndexOf(me_array, "a"));
// This would display 'The index of the value d is at: -1'
self iPrintLn("The index of the value d is at: " + _array_IndexOf(me_array, "d"));
}
*/
_array_IndexOf(arr, obj)
{
if(isArray(arr) && isDefined(obj))
for(i = 0; i < arr.size; i++)
if(arr[i] == obj)
return i;
return -1;
}
/*
Returns an array with all its items removed.
Arguments:
arr - An array to clear.
Example:
Not needed, too straight forward.
*/
_array_Clear(arr)
{
// Might be a little useless but hey, oh wells.
if(isArray(arr))
for(i = arr.size - 1; i <= 0; i--)
arr[i] delete();
return arr;
}
/*
Returns an array with an object insert at a specific index.
If the index is the same value as the array size, it is added to the end.
Arguments:
arr - An array passed through as an argument.
index - The index from where to insert the object.
obj - The object to insert.
Example:
some_function()
{
me_array = [];
me_array[0] = "a";
me_array[1] = "b";
me_array[2] = "c";
me_array = _array_InsertAt(me_array, 0, "e");
for(i = 0; i < me_array.size; i++)
self iPrintLn(me_array[i]);
// The above will output:
// e
// a
// b
// c
}
*/
_array_InsertAt(arr, index, obj)
{
if(isArray(arr) && arr.size > 0 && isDefined(index) && index >= 0 && index <= arr.size && isDefined(obj))
{
if(index == arr.size)
{
arr[arr.size] = obj;
}
else
{
arr[arr.size] = 0;
for(i = arr.size - 2; i >= index; i--)
arr[i+1] = arr[i];
arr[index] = obj;
}
}
return arr;
}
Note that these functions were inspired by the IList Interface and Array Class from the .NET Framework.
A casual conversation between barata and I about Nukem.