Array madness. What am I doing wrong?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
vbmark
Posts: 36
Joined: 30 Nov 2013, 22:17

Array madness. What am I doing wrong?

09 Dec 2013, 15:38

Code: Select all

Loop, Read, MyFile.txt
{
	IfInString, A_LoopReadLine, `,
	{
		word_array := StrSplit(A_LoopReadLine, ",")
		MsgBox, %word_array[1]% ; Gives illegal character message.
		MsgBox, Blah % word_array[1] ; Error of missing ending percent sign.
		MsgBox, Blah %word_array[1]% ; Gives illegal character message.
		MsgBox, % word_array[1] ; Works

		if %word_array[1]% = %1% ; No combination I try works here
		{
			MsgBox, Found!
		}
	}
}
Thanks!
Guest

Re: Array madness. What am I doing wrong?

09 Dec 2013, 15:43

(Now I can understand why you want to hide your source code :D )

MsgBox, % word_array[1]
or
MsgBox % "Blah " word_array[1]

and

if (word_array[1] = 1)

I'm not sure what you want with %1% if that is meant to parse a command line parameter you might be better off assigning it to a normal variable at the very start of your script:

param := %1%

so it becomes

if (word_array[1] = param)
vbmark
Posts: 36
Joined: 30 Nov 2013, 22:17

Re: Array madness. What am I doing wrong?

09 Dec 2013, 16:09

Guest wrote:(Now I can understand why you want to hide your source code :D )
LOL
Guest wrote:
MsgBox, % word_array[1]
or
MsgBox % "Blah " word_array[1]

and

if (word_array[1] = 1)

I'm not sure what you want with %1% if that is meant to parse a command line parameter you might be better off assigning it to a normal variable at the very start of your script:

param := %1%

so it becomes

if (word_array[1] = param)
Thanks, this works great.

On a different note... the documentation says %0% contains the number of parameters passed (0 if none).

But %1% is blank and %0% contains my passed in parameter.
User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Array madness. What am I doing wrong?

09 Dec 2013, 16:31

Array?

Code: Select all

array := object()
array[1]:="element1"
array[2]:="element2"
??
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Array madness. What am I doing wrong?

09 Dec 2013, 19:20

vbmark wrote:On a different note... the documentation says %0% contains the number of parameters passed (0 if none).

But %1% is blank and %0% contains my passed in parameter.
The documentation also says that variables in expressions should not be enclosed in percent signs, and that you can't directly reference the command line args in an expression. %0% in an expression refers to the last command line arg, because the variable '0' contains the number of args = the name of the last arg variable.
User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Array madness. What am I doing wrong?

09 Dec 2013, 22:47

I know of that... But I must admit, this can be a little bit confusing... ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
vbmark
Posts: 36
Joined: 30 Nov 2013, 22:17

Re: Array madness. What am I doing wrong?

10 Dec 2013, 07:31

lexikos wrote:...because the variable '0' contains the number of args = the name of the last arg variable.
I'm not sure I understand how %0% can contain both the number of arguments and the last argument.
User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Array madness. What am I doing wrong?

10 Dec 2013, 08:15

No, %0% does contain the number of arguments. If it is equal to one of your parameters, then it is possibly coincidence, or you've somehow changed the value of %0%.
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
AfterLemon
Posts: 85
Joined: 30 Sep 2013, 14:27
Location: Ohio, USA

Re: Array madness. What am I doing wrong?

12 Dec 2013, 10:57

Lexikos is saying something along the lines that these examples will describe:

Code: Select all

A=%0% 	; Will contain the passed amount of parameters (which is ALSO the final var containing passed parameters)
 				; For example, if passing 7 parameters, %0% will contain 7 which is the last passed var %7%.

A:=0			; Simply assigns "A" the value of 0 (or essentially null in certain circumstances)

A:=%0%	; Passes the value contained in 0 (i.e. the total number of parameters [which is equivalent to the last parameter filled] as shown in example 1)
	  			; as a variable. This means that if 7 parameters are passed, it will pass whatever is in the 7 variable.

A:=7			; Though intuitively this would allow passing the same as the above, this is not the case, as it passes the number 7 only.

A:=%1%	; This variable contains the data of the passed parameter 1, and that data does not have a "value" so the double-dereferencing will result in an empty assignment.
Given, for example, that 1 is passed as a parameter with the string "Parameter1Data" on running, using
Parameter1Data:="This Works"
Will allow the previous example A:=%1% to correctly be filled.

Code: Select all

If (A="This Works")
	Msgbox
Someone please correct me if I'm wrong.
HOME: Windows 11 Pro | AMD Ryzen 7 5800X 8-Core @ 4.50GHZ | 64GB RAM
MOBILE: Samsung Galaxy Note 10+
vbmark
Posts: 36
Joined: 30 Nov 2013, 22:17

Re: Array madness. What am I doing wrong?

12 Dec 2013, 11:15

Good stuff. Thank you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 210 guests