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: 
coredump
Visitor

Associative Array Errors - n00b

I am trying to create an associative array of coordinates, something like { {x:40,y:50}, {x:40,y:51}} , etc.


a_poster_pos = CreateObject("roAssociativeArray")

' l_x and l_y are incremented in a loop
position = {}
position["x"] = l_x;
position["y"] = l_y;

a_poster_pos.Append(position);



Since I am new to this, it of course doesn't work and I get "Syntax Error" compilation errors
0 Kudos
22 REPLIES 22
RokuMarkn
Visitor

Re: Associative Array Errors - n00b

Don't end a line with a semicolon in brightscript.

I can't tell from your description but I suspect what you actually want is an array of AAs. You want an arbitrary number of AAs in your top level container, right? Make it an array and Push each AA onto it.

--Mark
0 Kudos
Komag
Roku Guru

Re: Associative Array Errors - n00b

I wouldn't even do AA, just make it an array of arrays

a_poster_pos = []

' l_x and l_y are incremented in a loop
position = []
position.Push(l_x)
position.Push(l_y)

a_poster_pos.Push(position)

later you know that each array has two values, in slots 0 and 1, which you can check
a_poster_pos[0][0] would give you the x value for position 1
a_poster_pos[12][1] would give you the y value for position 13
0 Kudos
coredump
Visitor

Re: Associative Array Errors - n00b

If I do it this way:


a_poster_pos = []

' l_x and l_y are incremented in a loop
position = []
position.Push(l_x)
position.Push(l_y)

a_poster_pos.Push(position)


How can I get the x and y coordinates iterating through a for loop ?


for each coordinate in a_poster_pos
print "x : ";coordinate[].x
print "y : ";coordinate[].y
end for
0 Kudos
belltown
Roku Guru

Re: Associative Array Errors - n00b

I think this is what you're looking for:


a_poster_pos = []

' l_x and l_y are incremented in a loop
a_poster_pos.Push({x: l_x, y: l_y})



for each coordinate in a_poster_pos
print "x : ";coordinate.x
print "y : ";coordinate.y
end for


EDIT: You can use arrays instead of AAs, as Komag suggested, although I wouldn't recommend it unless you have some extremely compelling performance reason for using a numeric-based array index. It's much better programming practice to call the y-coordinate "y" and the x-coordinate "x", rather than 0 and 1 (or was it 1 and 0 -- see what I mean?). That might work for baby programs, but can really mess you up as programs become larger and more complex.
0 Kudos
Komag
Roku Guru

Re: Associative Array Errors - n00b

for each coordinate in a_poster_pos
print "x : ";coordinate[0]
print "y : ";coordinate[1]
end for


No need to use an AA, just adds what appears to be unnecessary complexity, unless it's more clear to you that way to have the x and y labels written there every time, then go ahead
0 Kudos
coredump
Visitor

Re: Associative Array Errors - n00b

I get a type mismatch with that for loop at this line :


Type Mismatch. (runtime error &h18) in pkg:/source/main.brs(95)
095: print "x : ";coordinate[0]
0 Kudos
belltown
Roku Guru

Re: Associative Array Errors - n00b

"coredump" wrote:
I get a type mismatch with that for loop at this line :


Type Mismatch. (runtime error &h18) in pkg:/source/main.brs(95)
095: print "x : ";coordinate[0]

I think it's telling you that 'coordinate' is not an array. Check that it's been set up correctly. Before this statement, add:
print coordinate
print type (coordinate)
to see if it actually prints out some array items.

Although you might want to go back and read my previous post, including the edit I added after posting.
0 Kudos
TheEndless
Channel Surfer

Re: Associative Array Errors - n00b

"Komag" wrote:
for each coordinate in a_poster_pos
print "x : ";coordinate[0]
print "y : ";coordinate[1]
end for


No need to use an AA, just adds what appears to be unnecessary complexity, unless it's more clear to you that way to have the x and y labels written there every time, then go ahead

I'm with belltown on this one. Using an AA allows you to easily keep track of what you're storing, and allows you to easily add new fields should you decide you need to in the future (e.g., a corresponding width and height), without having to worry about remembering what each index in the array is storing. An array of arrays is much more complex to manage, and a lot harder to decipher should you need to come back to the code later, in my opinion.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
Komag
Roku Guru

Re: Associative Array Errors - n00b

That makes sense, clarity with labeling
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.