Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
btpoole
Level 7

Check Value roArray

Is there a quick way to check for a value in a roArray? Something like in_array?
0 Kudos
4 Replies
Komag
Level 9

Re: Check Value roArray

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
0 Kudos
btpoole
Level 7

Re: Check Value roArray

Much appreciated, thought maybe Brightscript had a function but yours works very well. Thanks again.
0 Kudos
EnTerr
Level 8

Re: Check Value roArray

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...)
0 Kudos
Roku Employee
Roku Employee

Re: Check Value roArray

Hi @Enterr, those are good suggestions, and will be taken into consideration if/when possible. Smiley Happy
0 Kudos