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: 
360tv
Streaming Star

What is the deal with this array print?? Weird.

Usually when I have a problem, it's something easy that I'm overlooking or some little detail I wasn't aware of. I'm sure this is the case here. I have an XML;


<feed>
<setmusic y="75" h="120">
<option>
<sd font="0" size="1" X="50" Y="100" W="180" H="100" />
<hd font="0" size="1" X="50" Y="100" W="340" H="180" />
<color>000066FF</color>
<selected>0000FFFF</selected>
<text>All Music Random</text>
<textcolor>FFFFFFFF</textcolor>
</option>
<option>
<sd font="0" size="1" X="260" Y="100" W="180" H="100" />
<hd font="0" size="1" X="50" Y="100" W="340" H="180" />
<color>000066FF</color>
<selected>0000FFFF</selected>
<text>List 1 Random</text>
<textcolor>FFFFFFFF</textcolor>
</option>
<option>
<sd font="0" size="1" X="480" Y="100" W="180" H="100" />
<hd font="0" size="1" X="50" Y="100" W="340" H="180" />
<color>000066FF</color>
<selected>0000FFFF</selected>
<text>List 2 Random</text>
<textcolor>FFFFFFFF</textcolor>
</option>
</setmusic>
</feed>


and my script is:

for idx = 0 to (homexml.setmusic.option.count() -1 )
item.text = homexml.setmusic.option[idx].text.gettext()
item.textcolor = homexml.setmusic.option[idx].textcolor.gettext()
print ""
print idx.tostr() "-------"
items.push(item)
print homexml.setmusic.option[idx].text.gettext()
print items[idx].text
print "--------"

next

for idx = 0 to (homexml.setmusic.option.count() -1 )
print ""
print idx.tostr() "++++++++"
print homexml.setmusic.option[idx].text.gettext()
print items[idx].text
print "+++++++++"
next

print "-------"
print items[0].text
print items[1].text
print items[2].text
print "-------"


which puts out:


0-------
All Music Random
All Music Random
--------

1-------
List 1 Random
List 1 Random
--------

2-------
List 2 Random
List 2 Random
--------

0++++++++
All Music Random
List 2 Random
+++++++++

1++++++++
List 1 Random
List 2 Random
+++++++++

2++++++++
List 2 Random
List 2 Random
+++++++++

-------
List 2 Random
List 2 Random
List 2 Random
-------


so the issue is, when I do print items[idx].text the first time it prints correctly. when I do it again, it does not. Also incorrect when I pick an array index manually. How does it get to the point that it prints the same text for all three instead of the different three texts?
0 Kudos
1 REPLY 1
EnTerr
Roku Guru

Re: What is the deal with this array print?? Weird.

Oooh, this one was a doozy!
In the first loop you kept modifying the very same `item`, so at the end all `items[]` ends up pointing to exactly the same element. You have to create new `item` on every iteration. Say by inserting `item = { }` on the next line after `for idx...`.

A more elegant way to do the same is
   for each option in homeXml.setmusic.option
item = {
text: option.text.getText(),
textcolor = option.textcolor.getText()
}
items.push(item)
'print option.text.getText()
'print item.text
'print "--------"
next
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.