Page 1 of 1

Array madness. What am I doing wrong?

Posted: 09 Dec 2013, 15:38
by vbmark

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!

Re: Array madness. What am I doing wrong?

Posted: 09 Dec 2013, 15:43
by Guest
(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)

Re: Array madness. What am I doing wrong?

Posted: 09 Dec 2013, 16:09
by vbmark
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.

Re: Array madness. What am I doing wrong?

Posted: 09 Dec 2013, 16:31
by joedf
Array?

Code: Select all

array := object()
array[1]:="element1"
array[2]:="element2"
??

Re: Array madness. What am I doing wrong?

Posted: 09 Dec 2013, 19:20
by lexikos
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.

Re: Array madness. What am I doing wrong?

Posted: 09 Dec 2013, 22:47
by joedf
I know of that... But I must admit, this can be a little bit confusing... ;)

Re: Array madness. What am I doing wrong?

Posted: 10 Dec 2013, 07:31
by vbmark
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.

Re: Array madness. What am I doing wrong?

Posted: 10 Dec 2013, 08:15
by joedf
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%.

Re: Array madness. What am I doing wrong?

Posted: 12 Dec 2013, 10:57
by AfterLemon
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.

Re: Array madness. What am I doing wrong?

Posted: 12 Dec 2013, 11:13
by joedf
perfect! ;)

Re: Array madness. What am I doing wrong?

Posted: 12 Dec 2013, 11:15
by vbmark
Good stuff. Thank you.