Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
btpoole
Channel Surfer

Array Range

Is there any way to extract a set number of values stored in an array. For example in the the array myarray I only want the first 10 entries in the array.
Thanks
0 Kudos
6 REPLIES 6
RokuMarkn
Visitor

Re: Array Range

I don't think there's anything simpler than the obvious

first_ten = []
for i = 0 to 9
first_ten.Push(my_array[i])
end for


--Mark
0 Kudos
EnTerr
Roku Guru

Re: Array Range

@btpoole, did you mean if there is an elegant way to do that, like array slicing in Python:
>>> a = [00,11,22,33,44]
>>> a[2:4]
[22, 33]
>>> a[3:]
[33, 44]
>>> a[:-2]
[0, 11, 22]
>>> a[::-2]
[44, 22, 0]

The answer would be "no". Good idea for future enhancement though, to add ifArray.slice(...)
0 Kudos
EnTerr
Roku Guru

Re: Array Range

I just thought, writing a universal slice function won't be hard - and here is what i sketched:

function arraySlice(arr, start=invalid, finish=invalid, step_=1):
if step_ = 0 then print "ValueError: slice step cannot be zero" : STOP
if start = invalid then if step_ > 0 then start = 0 else start = arr.count() - 1
if finish = invalid then if step_ > 0 then finish = arr.count() - 1 else finish = 0
if start < 0 then start = arr.count() + start 'negative counts backwards from the end
if finish < 0 then finish = arr.count() + finish
res = [ ]
for i = start to finish step step_:
res.push(arr[i])
end for
return res
end function

And here is testing it to act like the python examples above:

BrightScript Debugger> a = [00,11,22,33,44]
BrightScript Debugger> ? arraySlice(a, 2, 3)
22
33

BrightScript Debugger> ? arraySlice(a, 3)
33
44

BrightScript Debugger> ? arraySlice(a, invalid, -3)
0
11
22

BrightScript Debugger> ? arraySlice(a, invalid, invalid, -2)
44
22
0


Just like in Python it can slice forward - or backwards (-1 step reverses the slice) - or from the beginning to certain point - or from a position till the end - or every N-th element (when step is N>1). One difference is that it's inclusive of the slice end, it's easier for beginners that way (and was easier for me to write too).
0 Kudos
btpoole
Channel Surfer

Re: Array Range

Thanks for the input. I was hoping I was missing a part in sdk that provided a dot operator where a range could be extracted. I can live with the small code to do it, but was looking for a one liner.
Thanks again
0 Kudos
EnTerr
Roku Guru

Re: Array Range

"btpoole" wrote:
I can live with the small code to do it, but was looking for a one liner.

I just wrote you a little gem* of a function that does in one line what you asked for:
mySlice = arraySlice(myArray, 0, 9)  'returns the 10 first elements 
Is that not good enough? It even does party tricks, like say when called with no extra params, `arraySlice(arr)` makes a copy of the array.

Or would you like this functionality added to roArray, presumably in optimized implementation? You and me both, brother... where do i sign? 😉

(*) all credit goes to
a) Python's slice notation/concept
b) optional parameters capability in BrightScript
c) the way FOR loop loops
0 Kudos
btpoole
Channel Surfer

Re: Array Range

EnTerr, thanks a million. I had created the script using the for/end for but will use this in its place.
Thanks again
0 Kudos