PDA

View Full Version : [CoD2] isArray(var)



kung foo man
1st November 2012, 14:51
Since their is no isArray(var)-function in Call of Duty 2, this seems to work fine:



isArray(var)
{
return !isString(var) && isDefined(var.size);
}

IzNoGoD
1st November 2012, 16:52
Or just remember whether or not a certain variable was an array or not...

Tally
1st November 2012, 18:20
What would be a nice function for COD2 is a getArrayKeys(). Which is a function in COD4 onwards. This returns the KEY - not the ELEMENT/VALUE - in an array (and if you don't know the difference between them, you shouldn't be reading this post). I have been trying to code one using COD script for over 3 months and can't get one to work 100% on all types of key (not all keys are ints. Some are strings). PHP has the very nice and handy array_keys() function. And COD4 has the getArrayKeys(). But poor old COD2 doesn't have anything which will return them. I suspect that COD script is just too limited in what it can parse in an array, and what it can't. However, I am willing to happily see someone prove me wrong on this.

Tally
1st November 2012, 18:36
I'd thought I'd flesh out what I mean by this function. This is the PHP array_keys() function, and a couple of examples, and the returned output:


array_keys( array $input, mixed $search_value = NULL, bool $strict = false )

Required Args:
1: < array $input > - An array containing keys to return.

Additional Args:

2: < mixed $search_value = NULL > - If specified, then only keys containing these values are returned.
3: < bool $strict = false > - Determines if strict comparison (===) should be used during the search.


INPUT:



<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
print_r(array_keys($array));
?>


OUTPUT:



Array
(
[0] => 0
[1] => color
)
Array
(
[0] => 0
[1] => 3
[2] => 4
)
Array
(
[0] => color
[1] => size
)


The COD4 getArrayKeys() seems to operate with 1 argument and returns the key with its corresponding value (element). When using this function, the array is flipped. So, you have to count down from the top of the array like this:


{

keys = getArrayKeys( array );
for( i = keys.size-1; i >= 0; i-- )
{
if( arg == keys[i] )
return true;

return false;
}

}


It would be really nice and handy to have a function like this in COD2. Only, as I said, there is no way to parse the keys in an array with COD script, only the value. Someone please prove me wrong!

kung foo man
2nd November 2012, 05:11
Or just remember whether or not a certain variable was an array or not...

Needed it for introspection of different return-values. ;P


I guess the keys are only obtainable through script engine hacking or writing an annoying array-layer. :S