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

Hold Array Value

I have a function with several conditional statements. In one of the statements, an array is created and assigned a value. I call the function later in code and enter the second conditional statement. I wish to use the value of the array that was created and populated in the first conditional statement, problem is apparently an array will loose it's value once the function is exited after the first conditional statement. How can I hold the value of the array to be used later?
Thanks
0 Kudos
11 Replies
Komag
Level 9

Re: Hold Array Value

I use an array that is contained within an associative array that is within the MAIN function, so it never goes out of scope.

I have one associative array called mAA (movement Associative Array)

Within it I have many keys and values, many of which are arrays. Maybe one is set up to be an empty array, such as mAA.lights = []

So later, in a function, I send mAA along such as
FUNCTION thisFunction(mAA)
do this
to that
mAA.lights.Push("light1")
END FUNCTION

Then later, within MAIN repeating WHILE loop, I can read mAA.lights and see that it's not empty and that the first value is "light1"
0 Kudos
btpoole
Level 7

Re: Hold Array Value

Thanks Komag. What I actually have is a roAssociativeArray in a roArray. I am parsing an xml file into the roAssociativeArray when the channel loads, then the roAssociativeArray is pushed into roArray. Once executing, if a remote button is pressed (up or down) a condition variable is set and the function is called. The value of the condition variable determines what conditional statement is entered. In this case variable=2. The conditional statement is entered, the value of the roArray is pulled up to be used in this part of the function. But as I have discovered, once the function exits after the initial start, the roArray is empty so when I entered the conditional statement of variable=2 I get dot operator error because the program does not see it as roArray and populated. I can make the code work by waiting to parse the xml and creating the roAssociativeArray, roArray and using the value in the function (part of function involves converting dates and times) all at one time but it is slow in doing all especially if xml gets large. I was hoping to create the roArray at startup one time then pull from it as needed. I see the roArray as a striped down of the xml file with only what I need.
0 Kudos
squirreltown
Level 9

Re: Hold Array Value

Any reason you can't do m.array = [etc.] instead of array = [etc.] ? Then it won't disappear when the function exits.
Kinetics Screensavers
0 Kudos
btpoole
Level 7

Re: Hold Array Value

Thanks squirreltown but for some unknown reason I can't get it to pass.
Example


function pxml()
for i= 0 to anothervalue.count()-1
associativearray={
x: value1[i],
y: value2[i],
z: value3[i]
}
xarray.Push(associativearray)
m.myarray=xarray
end function

function getmyarray()
plist=[]
plist=m.myarray
?"Count of plist. . ."plist.count()
end function
end for


There is no trouble populating the xarray, but once out of the function it goes away. I have tried to call the xarray in the other function but get error. I have assigned it to another global (m.myarray=xarray) and get same result. I know it can be done but I am lost.
Thanks again

Edit:
I have attempted the above numerous times, numerous ways. Each time the function exits, the global array value is no longer there. In the function getmyarray(), Dot operator error occurs on the second line.
0 Kudos
squirreltown
Level 9

Re: Hold Array Value

Ok i'm not the smartest critter around here but why are you making an array, then making it equal to m.something?
Why not just make xarray - m.xarray from the start?
Then you are defining plist twice, first as an array, then as an array of AA's, don't you want to: plist.Push(m.myarray) ?
Kinetics Screensavers
0 Kudos
btpoole
Level 7

Re: Hold Array Value

Thanks for the notice. I tried making m.array.Push(associativearray) and was able to populate it. Forget the plist=[] (forgot to take out). If I set m.myarray.Push(associativearray) in the first function and then plist.Push(m.myarray) in the second it never works. The m.myarray doesn't stay populated. If I eliminate some code should not the m.myarray be accessible in the function getmyarray() as shown below? This is basically what I started with and kept getting error messages, so I starting trying other things to pass the array to the next function.

function pxml()
for i= 0 to anothervalue.count()-1
associativearray={
x: value1[i],
y: value2[i],
z: value3[i]
}
m.myarray.Push(associativearray)
end function

function getmyarray()
?"Count of m.myarray. . ."m.myarray.count()
end function
end for
0 Kudos
RokuMarkn
Level 7

Re: Hold Array Value

I don't know what you're doing wrong since you haven't posted all your code, but here's a working example of what I think you're trying to do.


function pxml(values as Object)
m.myarray = []
for i = 0 to values.count()-1
aa = { x: values[i], y: values[i]*2, z: values[i]*3 }
m.myarray.Push(aa)
end for
end function

function getmyarray()
print "Count of m.myarray = "; m.myarray.count()
end function

function RunUserInterface()
pxml([42,43])
getmyarray()
end function
0 Kudos
btpoole
Level 7

Re: Hold Array Value

Thanks Mark. My code is identical to what you have in setup and procedure with the one exception. I am not calling the getmyarray() or in code below the FUNCTION HoldIndex(index) in the runuserinterface(). This function is called later on when user presses a remote button. Below is what I have. I have cut download alot of the code for simplicity but main steps are shown. The setup runs, calls GetData where xml is parsed into associativearray, an array (m.programlist) is created from the associativearray (programinfo). There is a step between GetData and channelloop that draws a canvas. Once canvas drawn channelloop waits for user input from remote. Once remote button pressed in channelloop if id=2, Function HoldIndex(index) is called. This is where it breaks. The m.programlist is not available to this function. If it were still populated I should be able to call m.programlist.count() as shown below and have a value print but I only get "interface not a member of Brightscript Component" error.


Sub RunUserInterface()
o = setup()
o.setup()
o.GetData()
o.channelloop()
End Sub

Function GetData() AS OBJECT
facade = CreateObject("roOneLineDialog")
facade.SetTitle("Retrieving Program Information. . .")
facade.ShowBusyAnimation()
facade.Show()
xmlxfer=CreateObject("roUrlTransfer")
xmlport=CreateObject("roMessagePort")
xmlxfer.SetMessagePort(xmlport)
tvguide="http://xxx.xxx.x.xx:xxxxx/myfile.xml"
xmlxfer.SetUrl(myfile)
response=xmlxfer.GetToFile("tmp:/status.temp3")
readxml=ReadAsciiFile("tmp:/status.temp3")
xml = CreateObject("roXMLElement")
IF xml.Parse(readxml)
?"XML PARASED. . ."
programinfo=[]
programlist=[]
For i= 0 to xml.programme.count()-1
programinfo={
date: xml.programme[i]@start,
time: xml.programme[i]@start,
title: xml.programme[i].title.gettext()
channel: xml.programme[i]@channel 'GET CHANNEL
}
?"SHOW DATE. .."programinfo.date
?"SHOW TIME. . ."programinfo.time
?"SHOW TITLE. . ."programinfo.title
?"SHOW CHANNEL. . ."programinfo.channel
m.programlist.Push(programinfo)
End For
?"PROGRAMLIST CREATED. . ."
?"PROGRAMLIST COUNT. . ."m.programlist.count()
else
?"DIDN'T PASS. . ."
END IF

End Function
Function ChannelLoop() as object

index=0
while true
msg = wait(0, m.port)
if msg <> invalid
if (msg.isRemoteKeyPressed())
id = msg.GetIndex()
if id=2
programinfo=HoldIndex(index)
'?"PROGRAMIFNO. . ."programinfo.count()
endif
end while
End Function



FUNCTION HoldIndex(index)
?"HOLDINDEX PROGRAMLIST COUNT. . ." m.programlist.count()
return m.programlist
end function
0 Kudos
RokuMarkn
Level 7

Re: Hold Array Value

The code you posted doesn't compile.

A block (such as FOR/NEXT or IF/ENDIF) was not terminated correctly. (compile error &hb5) in pkg:/source/main.brs(52)


--Mark
0 Kudos