Your answer comes back 4231 doesn't it?
1) Your if statement isn't firing, so you never insert the variable e into the array.
2) Why isn't it firing? If d=4 and b=2, doesn't d-b = 2? Well, sure, but you didn't use expression syntax.
If (array[2] - array[1] = 2). If you do that, it's still not right, because array[2] is actually b which is 2. array[1] is d which is 4, so it's actually 2-4=-2. If you either make it
If (array[1] - array[2] = 2) or
If (array[2] - array[1] = -2), that If statement will work.
3) However, that just gets you the result 4elephant231. Why? Because the value of a, b, c, and d are just numbers. You completely overwrote the previous values of foods.
4) Xeno demonstrates it, but you can get spaces by putting literal strings in your expressions. You can alternatively use
string := array[1] A_Space array[2] A_Space array[3] A_Space array[4] A_Space array[5]
My guess is you wanted to try to do some kind of manipulation in your actual code where you find a numerical difference between two parts of an array and want to insert a value in the middle of those. What you can do is double up your arrays. It's late and this is probably not the right way to go about this, but, what the heck.
Code: Select all
a:=[1,"apple"]
b:=[2,"banana"]
c:=[3,"carrot"]
d:=[4,"date"]
e:=[5,"elephant"]
array:=[d, b, c, a]
If (array[2][1] - array[1][1] = -2)
array.InsertAt(2,e)
string:=array[1][2] " " array[2][2] " " array[3][2] " " array[4][2] " " array[5][2]
MsgBox % string
return