|
Working with Arrays Ben Graham from United States [17 posts] |
12 year
|
STeven-
I was wondering what would be the best way to store and reverse an array in RR? I was thinking about using the "WRITE VARIABLE" module but the accuracy is too low. For example, I want to store motor values "255,255,255,0,255,0,0" and then reverse the array to read out "0,0,255,0,255,255,255". I was trying to use FOR loops in the VBSCRIPT module but have had little success.
Any help would be greatly appreciated,
-Ben
|
|
|
Anonymous |
12 year
|
Ben,
Perhaps the following would work?
list = GetArrayVariable("my_list")
if isArray(list) and ubound(list) > 0 then
top = ubound(list)
for i = 0 to ubound(list)/2
t = list(i)
list(i) = list(top)
list(top) = t
top = top - 1
next
end if
SetArrayVariable "my_list", list
The trick here is that the first and last element are just swapped ... which allows you to do an in-place reversal.
STeven.
|
|
|
Ben Graham from United States [17 posts] |
12 year
|
Thank you very much STeven that works perfectly.
One more array question. I want to store the values of a joystick into an array every time I press the button on the joystick. I want this array to be dynamic so I can store 1 value or X values.(Just wondering what is the maximum size of an array in RR?). I have tried using ReDim and Preserve but have had no luck. This is what I thought would work but has been unsuccessful:
trigger=GetVariable("trigger")
X_AXIS=GetVariable("X_AXIS")
ReDim list(i)
if trigger=1 then
i=i+1
list(X_AXIS)
else
i=i
end if
So every time the button(trigger) is pressed the array increases by 1 and makes the new entry the current value of the X_AXIS.
Thank you very much,
-Ben
|
|
|
Anonymous |
12 year
|
Ben,
Where does the "list" come from? You are missing a GetArrayVariable.
You would also need a check based on the last element otherwise this array would increase very quickly in size even in a single trigger press (humans are much slower than machines).
trigger=GetVariable("trigger")
X_AXIS=GetVariable("X_AXIS")
if trigger=1 then
list = GetArrayVariable("my_list")
size = ubound(list)
if list(size) <> X_AXIS then
ReDim list(size+1)
list(size+1) = X_AXIS
end if
SetArrayVariable "my_list", list
end if
You will get an extra 0 as the first number but you can just ignore that. If the array does not exist the GetArrayVariable will create an array with a single element of zero.
STeven.
|
|