Assigning values to variable names

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
speedskis777
Posts: 33
Joined: 28 Jan 2016, 17:24

Assigning values to variable names

24 May 2017, 22:17

Hi guys.

I'm trying to figure out how to assign variables a name, and then a value associated with it, such as the following... how can I get the msgbox to produce " dateelephantbananacarrotapple" as the answer? Also... how can I still use the = sign but have these words separated by a space (e.g. msgbox produces date elephant banana carrot apple).

Code: Select all

p::

a = apple
b = banana 
c = carrot
d = date
e = elephant

a := 1
b := 2
c := 3
d := 4

array := [d, b, c, a]
If array[2] - array[1] = 2
array.InsertAt(2, e)
string := array[1] . array[2] . array[3] . array[4] . array[5]
msgbox % string
Xeno234
Posts: 71
Joined: 24 Mar 2017, 18:14

Re: Assigning values to variable names

24 May 2017, 23:48

Code: Select all

p::

a = apple
b = banana 
c = carrot
d = date
e = elephant

msgbox % a b c d e
msgbox %a%%b%%c%%d%%e%
msgbox % a " " b " " c " " d " " e
msgbox %a% %b% %c% %d% %e%
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Assigning values to variable names

25 May 2017, 00:10

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: alawsareps, mebelantikjaya, mikeyww, RussF and 315 guests