Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Using A_Index variables


  • Please log in to reply
9 replies to this topic
tobadyurdead
  • Members
  • 173 posts
  • Last active: Oct 12 2013 02:08 AM
  • Joined: 04 May 2008
OK so i am using a loop to read an inifile and store the results like this
Loop %NOA%  
 { 
  iniread,Name%a_index%,Config.ini,Names,Name%a_index%   
 } 

and it stores all the variables fine, but im not sure how to use them...
Lets say:

NOA=3

Loop %NOA%  
 { 
  iniread,Name%a_index%,Config.ini,Names,Name%a_index%   
 } 

so the variables would be
Name%A_Index% (1)
Name%A_Index% (2)
Name%A_Index% (3)
...lost my train of thought ....
But, How would i make a Gui show all the variables, if there in not a predefined amount of "names" Iv tried

%Name%A_Index%%
But get a empty variable reference error or somthing because of that last pair of %%....Hw would i accomplish this?

Thanks

DataLife
  • Members
  • 955 posts
  • Last active: Today, 01:09 AM
  • Joined: 27 Apr 2008
Your variables would be

name1
name2
name3

name1 = One  ;I added these, but your iniread would declare these 3 variables
name2 = Two
name3 = Three

Loop %NOA%
 {
  text = % name%a_index%
  gui, add,text,, Variable %text% 
}
gui, show,
return


tobadyurdead
  • Members
  • 173 posts
  • Last active: Oct 12 2013 02:08 AM
  • Joined: 04 May 2008
apperently i didn't explain too well... there is a undefined amount of "names" it could be 1 , it could be 100, i am trying to use a loop to display them like this

***DOSEN"T WORK***
Gui,3: Font, S15 CDefault, Verdana
Gui,3: Add, Text, x104 y0 w180 h30 +BackgroundTrans, Statistics
Gui,3: Add, Text, x6 y30 w90 h30 +BackgroundTrans +Left, Name
Gui,3: Add, Text, x116 y30 w100 h30 +BackgroundTrans  +Center, Age
Gui,3: Add, Text, x226 y30 w100 h30 +BackgroundTrans  +Right, residense

Loop %NOA%  
 { 
Gui,3:Add,Text, %Name%a_index%%  ; I need a proper variable reference
 } 
 Gui,3: Show, x159 y99 h515 w390, Statistics
I cannot just use
Gui,3:Add,Text, %name1%
Gui,3:Add,Text, %name2%
Gui,3:Add,Text, %name3%
because ill be constantly adding new name to the ini file, and it would be inconvinient to modify the code everytime.

I hope this clears somethings up

DataLife
  • Members
  • 955 posts
  • Last active: Today, 01:09 AM
  • Joined: 27 Apr 2008
I just edited my post while you were replying.

The code to add to your loop is

text = % name%a_index%

tobadyurdead
  • Members
  • 173 posts
  • Last active: Oct 12 2013 02:08 AM
  • Joined: 04 May 2008
Ok , i think thats working.. for some reason the text displayed is ERROR..although i think that the error is on my side..ill post back to shoe my results. Thanks!


** Yeah my ini file was ****ed up..i fixed it and everything is workingperfect! Thanks again!

DataLife
  • Members
  • 955 posts
  • Last active: Today, 01:09 AM
  • Joined: 27 Apr 2008
If the iniread cannot read the key and section it returns ERROR. Verify the data is in the ini file and is referenced correctly with your iniread.

Also, just as a note. The following is valid. Kind of unusual to have a single percent sign but it is documented somewhere.

count = NewVarContents
var = % count

msgbox %var% ;variable now is NewVarContents



tobadyurdead
  • Members
  • 173 posts
  • Last active: Oct 12 2013 02:08 AM
  • Joined: 04 May 2008
Yeah i would of never thought to use an odd numberof % signs.... And yeah my ini wasmessed...Everything works fine now Thanks!

Lexikos
  • Administrators
  • 9391 posts
  • Last active:
  • Joined: 17 Oct 2006

Source: AutoHotkey Documentation: Expressions
Force an expression: An expression can be used in a parameter that does not directly support it (except an OutputVar or InputVar parameter such as those of StringLen) by preceding the expression with a percent sign and a space or tab. This technique is often used to access arrays. For example:

FileAppend, % MyArray%i%, My File.txt
MsgBox % "The variable MyVar contains " MyVar "."
Loop % Iterations + 1
WinSet, Transparent, % X + 100
Control, Choose, % CurrentSelection - 1

var = % expression works because A = B is implicitly a command with two parameters: A and B. I highly recommend using var := expression instead, as it is far more common.

tobadyurdead
  • Members
  • 173 posts
  • Last active: Oct 12 2013 02:08 AM
  • Joined: 04 May 2008

Source: AutoHotkey Documentation: Expressions
Force an expression: An expression can be used in a parameter that does not directly support it (except an OutputVar or InputVar parameter such as those of StringLen) by preceding the expression with a percent sign and a space or tab. This technique is often used to access arrays. For example:

FileAppend, % MyArray%i%, My File.txt
MsgBox % "The variable MyVar contains " MyVar "."
Loop % Iterations + 1
WinSet, Transparent, % X + 100
Control, Choose, % CurrentSelection - 1

var = % expression works because A = B is implicitly a command with two parameters: A and B. I highly recommend using var := expression instead, as it is far more common.



I wouldn't use somthing just because it is more common....is their other reasons why this method would be prefered?

Lexikos
  • Administrators
  • 9391 posts
  • Last active:
  • Joined: 17 Oct 2006

I wouldn't use somthing just because it is more common....is their other reasons why this method would be prefered?

It is more straightforward and intuitive to use :=, the expression assignment operator for its intended purpose. = is documented as assigning strings, and since some scripters may not see it as a command, they may not know how % applies to it. In other words, because it is more common, it is generally more easily understood.

var = % expression is internally translated into var := expression, as it is more efficient. However, var = % 1 + 2, var2 := 3 is not identical to var := 1 + 2, var2 := 3. The former is translated into expression-assignment, with "var" as the output var and "1 + 2, var2 := 3" as the expression, whereas the latter is translated to a standalone expression, "var1 := 1 + 2, var2 := 3". If the expression "1 + 2" were to somehow fail, var would be assigned an empty string for the former, or not assigned (i.e. left at its previous value) for the latter. There are probably other obscure differences that make it worth using the more common method.

Finally, := uses fewer characters and is probably easier to type.