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)


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


Upgrade to ARC Pro

Synthiam ARC Pro is a new tool that will help unleash your creativity with programming robots in just seconds!


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

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




@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.


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


Are you sure you have it as above?


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

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.


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

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()


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


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