AutoHotkey Community

It is currently May 27th, 2012, 1:41 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: January 13th, 2010, 10:54 pm 
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!!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 13th, 2010, 11:09 pm 
Offline

Joined: January 5th, 2008, 12:11 am
Posts: 102
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2010, 12:53 am 
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?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2010, 6:24 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2010, 6:38 am 
Offline

Joined: February 2nd, 2008, 4:35 am
Posts: 643
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2010, 3:17 pm 
Offline

Joined: January 5th, 2008, 12:11 am
Posts: 102
a_h_k wrote:
So does this work?

With an extra space between ahk_id and variable probably so :)
Code:
WinGet, program, ProcessName, % "ahk_id " WinID%A%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2010, 4:34 pm 
Offline

Joined: May 22nd, 2007, 1:06 am
Posts: 73
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2010, 8:12 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2010, 10:20 pm 
Offline

Joined: May 22nd, 2007, 1:06 am
Posts: 73
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 5:38 pm 
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++
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 6:39 pm 
Offline

Joined: November 16th, 2009, 9:24 am
Posts: 114
To be fair and honest, AHK's syntax is horrible.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 7:16 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
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.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 8:51 pm 
Offline

Joined: November 16th, 2009, 9:24 am
Posts: 114
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).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 8:59 pm 
Offline
User avatar

Joined: October 31st, 2008, 9:39 am
Posts: 641
Location: France
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 %


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 9:00 pm 
Offline

Joined: November 16th, 2009, 9:24 am
Posts: 114
The only good part of AHK's syntax is the key binding syntax... which is just as well, given "Hotkey" is in its name!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: bobbysoon, JSLover, Tipsy3000 and 21 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group