Asked — Edited

Help With Array

Good Afternoon.

I want to create an array where I store two string values. For this example I will call it $x and $y. I want to store the variables in the array. I want to store as many as I need. I understand the definearray command. What I do not understand is how to input in the array and keep adding the string variables. Let me post my example.

#Example Array



$x = GetRandomUnique(1,50)
$y = GetRandomUnique(1,100)

DefineArray($z,2)


#when I do this...
$x = $z[0]
$y = $z[1]
#the variables z0 and z1 stay the same everytime i run the script.



ARC Pro

Upgrade to ARC Pro

With ARC Pro, your robot is not just a machine; it's your creative partner in the journey of technological exploration.

#1  

I think you have it reversed... try this instead


$x = GetRandomUnique(1,50)
$y = GetRandomUnique(1,100)

DefineArray($z,2)

$z[0]=$x 
$z[1]=$y 


#2  

@Richard R I gave that a try and it seems to do the same thing. I am guessing I need some sort of way to shift or increment the string values into the array. I will keep working on it.

#3  

Hmm it worked perfectly for me... Every time I run the script $z[0] and $z[1] had different values....


$z[0]=$x 
$z[1]=$y 

Are you sure you have it as above?

#4  

Yes. I have it as above. For a brief moment the values are different. I am trying to learn about arrays and how to use them. I wonder if I need to use append array every time my script runs and then add a counter of some sort.

United Kingdom
#5  

Your variables were originally around the wrong way, Richard's reply was correct.

$variable = "Contents of variable"

So putting $x = $z[0] will cause $x to be the same as what $z[0] is, not the other way around.

Putting the variables around the correct way will solve the problem.

#6  

Thanks. Now how to you keep the data at $z[0] and $z[1] the same while adding more into the array for storage and do this for 100 times?

United Kingdom
#7  

Have you looked at the DefineArray and AppendArray examples in the Examples folder of ARC? They show how to use the array functions.

$z[0] and $z[1] wouldn't change if you use AppendArray()

To repeat it 100 times could be done a number of ways, the easiest is with a counter and a repeatuntil()

#8  

Yes I have looked at the examples. Thanks for the help.

#9  

I have two coding sugestions

1 remove the defarray statement out of the main loop of code and only issue the statement once in your code initialization at the startup before storing any values in the array.

2 you need a counter that increments 100 times and use that counters current value as the pointer to which value in the array you want to save or read