Forum Discussion

btpoole's avatar
btpoole
Channel Surfer
10 years ago

Check Value roArray

Is there a quick way to check for a value in a roArray? Something like in_array?
  • Not that I know of - you just have to search it.
    FUNCTION in_array(theArray, value)
    FOR EACH i IN theArray
    IF Type(i) = Type(value) AND i = value THEN RETURN TRUE
    END FOR
    RETURN FALSE
    END FUNCTION
  • btpoole's avatar
    btpoole
    Channel Surfer
    Much appreciated, thought maybe Brightscript had a function but yours works very well. Thanks again.
  • A better idea would be to add to ifArray:

    Index(x as Dynamic) as Integer
    Return the index in the list of the first item whose value is x, -1 if not found

    (All implementors of ifArray already do ifArrayGet/ifArraySet - also Delete() is harmonious on integer indexing)

    If having to check a long (>100) list though, suggest using a dictionary lookup (i.e. ifAssociativeArray.doesExist(), .lookup() ).

    By the way @RokuKC - any chance of fixing the equality/inequality operators, so that they don't puke "Type Mismatch"? I.e. "=" to return True only when both type and value of the arguments match - and False otherwise. Fair and square, no warnings no nothing - it's already the case in all script languages i can think of (python, javascript, lua, whathaveyou...)
  • RokuKC's avatar
    RokuKC
    Roku Employee
    Hi EnTerr, those are good suggestions, and will be taken into consideration if/when possible. 🙂