coredump
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
12:27 PM
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.
Since I am new to this, it of course doesn't work and I get "Syntax Error" compilation errors
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
22 REPLIES 22

RokuMarkn
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
12:30 PM
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
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

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
01:17 PM
Re: Associative Array Errors - n00b
I wouldn't even do AA, just make it an array of arrays
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
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
coredump
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
02:25 PM
Re: Associative Array Errors - n00b
If I do it this way:
How can I get the x and y coordinates iterating through a for loop ?
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
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
02:42 PM
Re: Associative Array Errors - n00b
I think this is what you're looking 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.
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.

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
02:44 PM
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
coredump
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
03:02 PM
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]
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
03:20 PM
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 coordinateto see if it actually prints out some array items.
print type (coordinate)
Although you might want to go back and read my previous post, including the edit I added after posting.

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
04:37 PM
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)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015
06:38 PM
Re: Associative Array Errors - n00b
That makes sense, clarity with labeling