says unneeded { and } but idk why? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
wryyymuda
Posts: 32
Joined: 21 Sep 2018, 21:43

says unneeded { and } but idk why?

31 Jan 2019, 20:51

this is the code, right now i'm trying to make a file that reads a text doc and generates a random line from it but i ran into an unexpected error, at lines 8 and 11 it says } is unexpected but i'm extremely confused by this. can anyone help?

Code: Select all

#singleinstance, force

inputbox, file, input, which .txt file 
+.::
if ( file > 0 ) {
	Loop, Read, %file% {
		lines = %A_Index% 
		}
		msgbox, %lines%
		return
	}
else if ( file = file) {
	msgbox, no file selected!
	ExitApp
	}
gregster
Posts: 4642
Joined: 30 Sep 2013, 06:48

Re: says unneeded { and } but idk why?  Topic is solved

31 Jan 2019, 21:21

The one-true-brace style (block's opening brace on the same line as the block's controlling statement rather than underneath on a line by itself) is only allowed for normal loops:
https://autohotkey.com/docs/commands/Block.htm#otb
https://autohotkey.com/docs/commands/Loop.htm#Remarks
this means, you will have to use:

Code: Select all

Loop, Read, %file% 
{
Last edited by gregster on 31 Jan 2019, 21:25, edited 1 time in total.
wryyymuda
Posts: 32
Joined: 21 Sep 2018, 21:43

Re: says unneeded { and } but idk why?

31 Jan 2019, 21:24

gregster wrote:
31 Jan 2019, 21:21
The one-true-brace style (block's opening brace on the same line as the block's controlling statement rather than underneath on a line by itself) is only allowed for normal loops:
https://autohotkey.com/docs/commands/Block.htm#otb
https://autohotkey.com/docs/commands/Loop.htm#Remarks
this means, use:

Code: Select all

Loop, Read, %file% 
{
so it should be like

Code: Select all

loop, read, %file%
{
	lines= %A_index%
}
msgbox, lines in file = %lines%
thats how it works?
wryyymuda
Posts: 32
Joined: 21 Sep 2018, 21:43

Re: says unneeded { and } but idk why?

31 Jan 2019, 21:39

full code below, just reads a random line from txt file.

Code: Select all

#singleinstance, force
inputbox, file, input txt, file to read (press < to activate)
+.::
	if ( file > 0 ) 
	{
	Loop, Read, %file% 
		{
			lines = %A_Index% 
		}
	random, rngline , 1 , %lines%
	filereadline , text, %file%, %rngline%
	msgbox, %text%
	}
	else if ( 1 = 1) 
	{
		msgbox, no file selected!
		ExitApp
	}
Return
gregster
Posts: 4642
Joined: 30 Sep 2013, 06:48

Re: says unneeded { and } but idk why?

31 Jan 2019, 21:46

Just to be clear, the problem was only in the one Loop, Read line.
The brackets in the if-lines should have been ok, because you used expression style if (with parentheses: () )... but with brackets on its own lines you should be safe, if you are in doubt.

btw, else if ( 1 = 1) is redundant... because the if-test is always true here. Thus, you can just use else.

Also, a (text) file could be legally named 0 (without extension) - then, your if-statement might miss this special case. Better use if ( file != "" ) or something like this.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, anhnha, Bacon19331, Bing [Bot], casperharkin, littlegandhi1199, Marius I, sobuj53, stiuna, Xeo786 and 46 guests