AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to make a "composite" variable?
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
doug33
Guest





PostPosted: Wed Jan 13, 2010 9:54 pm    Post subject: How to make a "composite" variable? Reply with quote

Hello,

Let's say my array is "StateCity"
I want to retrieve StateCity1, but the "1" is actually a variable that is equal to 1

If A = 1, how do I write the variable so AHK will understand this?

I've tried StateCityA, StateCity%A%, %StateCity%%A%, none of which seem to work. Is this a "dynamic variable"? Thanks for the help!!
Back to top
Kellianjaxon



Joined: 04 Jan 2008
Posts: 102

PostPosted: Wed Jan 13, 2010 10:09 pm    Post subject: Reply with quote

Code:
A = 1
StateCity1 = one
StateCity2 = two
StateCity3 = three

var := StateCity%A% ; Expression (:=)
Msgbox % var

; or
A++ ; A = 2
var = % StateCity%A% ; Traditional (=)
Msgbox % var

var := "StateCity" A + 1
var := %var%
Msgbox % var

; etc
Back to top
View user's profile Send private message
doug33
Guest





PostPosted: Wed Jan 13, 2010 11:53 pm    Post subject: Reply with quote

Hmm, ok. Is there some reason why the % Var1%Var2% syntax works in some cases but not others?

ie,

Code:


;if you make a WinGet, WinID, List array...

MsgBox % WinID%A%  ;this works great

WinGet, program, ProcessName, ahk_id % WinID%A%  ;but this is a syntax error ("the following variable name contains an illegal character: WinID")



Why?
Back to top
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Thu Jan 14, 2010 5:24 am    Post subject: Reply with quote

Putting " % " at the beginning of a command's parameter forces AHK to see the rest of that parameter as an expression. For example:
Code:
; example: using a dynamic variable which holds a window's unique ID
WinGet, program, ProcessName, % "ahk_id " . WinID%A_Index%
; notice the literal string "ahk_id " has a space on the left.
; this expression resolves to ahk_id 0x12345678

; example: using dynamic variables in a gui command
Gui, Add, Button, % "x" ( GuiWidth // 2 - ButtonWidth // 2 ) " y" ( GuiHeight // 2 - ButtonHeight // 2 ) " w" ButtonWidth " h" ButtonHeight " gSomeLabel", Button Text
; This would add a button smack-dab in the middle of the gui, provided the gui was actually
; as wide and tall as the variables imply

; example: using dynamic variables in the click command. 'Click' is a bit strange in that it only has 1 parameter
Click, % (A_ScreenWidth // 2) " " (A_ScreenHeight // 2) " Left 2"
; double-clicks the middle of the screen, assuming 'coordmode, mouse ,screen' is in effect

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
a_h_k



Joined: 02 Feb 2008
Posts: 626

PostPosted: Thu Jan 14, 2010 5:38 am    Post subject: Reply with quote

So does this work?
Code:
WinGet, program, ProcessName, % "ahk_id" WinID%A%

Found this in
Help --> MsgBox wrote:
Code:
; By preceding any parameter with "% ", it becomes an expression. In the following example,
; math is performed, an array element is accessed, and a function is called.  All of these
; items are concatenated via the "." operator to form a single string displayed by MsgBox:
MsgBox % "New width for object #" . A_Index . " is: " . RestrictWidth(ObjectWidth%A_Index% * ScalingFactor)
Back to top
View user's profile Send private message Visit poster's website
Kellianjaxon



Joined: 04 Jan 2008
Posts: 102

PostPosted: Thu Jan 14, 2010 2:17 pm    Post subject: Reply with quote

a_h_k wrote:
So does this work?

With an extra space between ahk_id and variable probably so Smile
Code:
WinGet, program, ProcessName, % "ahk_id " WinID%A%
Back to top
View user's profile Send private message
tekkie2412



Joined: 22 May 2007
Posts: 73

PostPosted: Thu Jan 14, 2010 3:34 pm    Post subject: Reply with quote

Can anybody tell me why this doesnt work? Ive tried so many different combinations I have no idea. Also tried going from "clipboard" to a temporary variable, then into the array. If I add any spaces before/after any of the %'s I get errors when opening the script.

Code:

F1::
InputBox,times,How many?

x = 0
Loop, %times%
{
SetTitleMatchMode, 2
WinActivate, Excel
Send, ^c
Sleep, 200
%Sim%x% := clipboard
Sleep, 200
Send, {RIGHT}
Sleep, 200
Send, ^c
Sleep, 200
%Imei%x% := clipboard
Sleep, 200
Send, {DOWN}{LEFT}
Sleep, 200
x++
}
x = 0
Loop, %times%
{
   MsgBox, %Sim% x% - %Imei% x%
   x++
}
Return


Thanks in advance
Back to top
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Thu Jan 14, 2010 7:12 pm    Post subject: Reply with quote

You have to be more observant about how you create and call dynamic variables:

Code:
F1::
x = 0
InputBox,times,How many?
Loop, %times%
{
SetTitleMatchMode, 2
WinActivate, Excel
Send, ^c
Sleep, 200
Sim%x% := clipboard
Sleep, 200
Send, {RIGHT}
Sleep, 200
Send, ^c
Sleep, 200
Imei%x% := clipboard
Sleep, 200
Send, {DOWN}{LEFT}
Sleep, 200
x++
}
x = 0
Loop, %times%
{
   MsgBox, % Sim%x% - Imei%x% ; you must force expression mode to perform a math operation
   x++
}
Return

_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
tekkie2412



Joined: 22 May 2007
Posts: 73

PostPosted: Thu Jan 14, 2010 9:20 pm    Post subject: Reply with quote

Thanks for your response but it still doesnt work and Im not quite sure why.

All I get is a msgbox saying "Sim1 - Imei1" , "Sim2 - Imei2", etc.
Back to top
View user's profile Send private message
Guest






PostPosted: Fri Jan 15, 2010 4:38 pm    Post subject: Reply with quote

tekkie2412 wrote:
All I get is a msgbox saying "Sim1 - Imei1" , "Sim2 - Imei2", etc.

...check your percents!...(it works for me)...

Code:
x=0
Loop, 19 {
   Sim%x% := 19
   Imei%x% := 38
   x++
}
x=0
Loop, 19 {
   msgbox, Sim%x% - Imei%x%
   ;//     ^--- % missing = msgbox = Sim0 - Imei0
   msgbox, % Sim%x% - Imei%x%
   ;//     ^--- % present = msgbox = -19
   x++
}
Back to top
BoffinbraiN



Joined: 16 Nov 2009
Posts: 114

PostPosted: Fri Jan 15, 2010 5:39 pm    Post subject: Reply with quote

To be fair and honest, AHK's syntax is horrible.
Back to top
View user's profile Send private message Visit poster's website
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Fri Jan 15, 2010 6:16 pm    Post subject: Reply with quote

BoffinbraiN wrote:
To be fair and honest, AHK's syntax is horrible.

Not that much...
_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
BoffinbraiN



Joined: 16 Nov 2009
Posts: 114

PostPosted: Fri Jan 15, 2010 7:51 pm    Post subject: Reply with quote

Well that's my opinion, and the opinion of a person who has experience programming in a dozen languages. I could go on and give all the reasons, but whatever - I just hope the syntax is tightened in up a future edition (which will also make it easier to learn, and hence this thread would never exist).
Back to top
View user's profile Send private message Visit poster's website
Zaelia



Joined: 31 Oct 2008
Posts: 604
Location: France

PostPosted: Fri Jan 15, 2010 7:59 pm    Post subject: Reply with quote

I know only AHK, and it sure is ugly when we need to write everything on a single line, however, it becomes extremely simple when we written intermediate lines to avoid more %
Back to top
View user's profile Send private message Send e-mail
BoffinbraiN



Joined: 16 Nov 2009
Posts: 114

PostPosted: Fri Jan 15, 2010 8:00 pm    Post subject: Reply with quote

The only good part of AHK's syntax is the key binding syntax... which is just as well, given "Hotkey" is in its name!
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group