Results 1 to 5 of 5

Thread: [CoD2] isArray(var)

  1. #1
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,011
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts

    [CoD2] isArray(var)

    Since their is no isArray(var)-function in Call of Duty 2, this seems to work fine:

    Code:
    isArray(var)
    {
        return !isString(var) && isDefined(var.size);
    }
    timescale 0.01

  2. The Following User Says Thank You to kung foo man For This Useful Post:

    RobsoN (31st May 2013)

  3. #2
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    Or just remember whether or not a certain variable was an array or not...

  4. #3
    Brigadier General
    Join Date
    Oct 2012
    Posts
    994
    Thanks
    20
    Thanked 588 Times in 388 Posts
    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.
    Last edited by Tally; 1st November 2012 at 17:23.

  5. The Following User Says Thank You to Tally For This Useful Post:

    kung foo man (2nd November 2012)

  6. #4
    Brigadier General
    Join Date
    Oct 2012
    Posts
    994
    Thanks
    20
    Thanked 588 Times in 388 Posts
    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:

    Code:
    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:

    Code:
    <?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:

    Code:
    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:

    Code:
    {
    
    	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!
    Last edited by Tally; 1st November 2012 at 18:05.

  7. #5
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,011
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    Quote Originally Posted by IzNoGoD View Post
    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
    timescale 0.01

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •