Limit on amount of lists in a script? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Limit on amount of lists in a script?

Post by jarhead » 29 Nov 2021, 21:22

I have a script that starts with a GUI containing two DropDownLists and some other text fields. The first DropDownList allows you to choose a state and then depending on your choice, displays additional results in the second DropDownList. For each state, I've added a list like the following:

Code: Select all

Alabama := "Birmingham|Montgomery|Tuscaloosa"
The script has been working but for some reason, when I get to North Carolina, the script fails and tells me...
Error at line 39.
Line Text: North Carolina := "Asheville|Durham|Fayetteville|Salisbury"
Error: This line does not contain a recognized action.
The program will exit.
The beginning of the script contains lists like the following:

Code: Select all

Alabama := "Birmingham|Montgomery|Tuscaloosa"
Alaska := "Anchorage"
Arizona := "Phoenix|Prescott|Tucson"
It is followed by:

Code: Select all

Gui, +AlwaysOnTop
Gui, Add, Text, x15, Choose State:
Gui, Add, DropDownList, x15 w190 gChooseState vState, Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|New Jersey|New Mexico|New York|North Carolina|North Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming
Gui, Add, Text, x15, Facility:
Gui, Add, DropDownList, x15 w190 gChoose vFacility

Gui, Add, Button, x170 y190 w35 h20 +default, OK
Gui, Add, Button, x120 y190 w45 h20, Cancel
Gui, Show, w220 h220, Test
Return

ChooseState:
GuiControlGet, State
GuiControl,, Facility, % "|" %State%
Return

choose:

gui, submit, nohide

; Alabama
if (Facility="Birmingham")
	INFO := "Birmingham additional info"
if (Facility="Montgomery")
	INFO := "Montgomery additional info"
if (Facility="Tuscaloosa")
	INFO := "Tuscaloosa additional info"
	
return

GuiClose:
ButtonCancel:
ExitApp

ButtonOK:
Gui, Submit
Since the script works great from Alabama through New York and then fails with North Carolina, I'm assuming there is a limit on how many lists you can have in a script? The rest of the script then inserts the information into a Word document and saves it as both Word & PDF. Any ideas? Thanks!

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Limit on amount of lists in a script?

Post by gregster » 29 Nov 2021, 21:35

jarhead wrote:
29 Nov 2021, 21:22
Error at line 39.
Line Text: North Carolina := "Asheville|Durham|Fayetteville|Salisbury"
Error: This line does not contain a recognized action.
The program will exit.
A variable name mustn't have a real space character in it.

I can't imagine that New York works like intended, although it won't throw an error, because the keyword new has a special meaning in AHK.
You will probably end up it with a variable named York...

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Limit on amount of lists in a script?

Post by jarhead » 29 Nov 2021, 22:08

gregster wrote:
29 Nov 2021, 21:35
A variable name mustn't have a real space character in it.
Thanks! That would explain it. You mentioned a real space character... is it possible to remove the space and replace with code so the two word states can still be used as variables?

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Limit on amount of lists in a script?

Post by gregster » 29 Nov 2021, 22:27

Sure, you could always use an underscore _ or some other visible - and allowed - character, in the variable name instead of a space(, or no character at all).
(I strongly advise against using other special (and also invisble) space characters.)
Replace the underscore, or the space, vice versa, whenever needed (-> StrReplace()). Eg, if you choose New York from the DDL, just replace all spaces (one) with a _ - then you have the variable name you want to look up: New_York. Do this for all state names that contain spaces.

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Limit on amount of lists in a script?

Post by jarhead » 29 Nov 2021, 22:40

gregster wrote:
29 Nov 2021, 22:27
Sure, you could always use an underscore _ or some other visible - and allowed - character, in the variable name instead of a space, or with no character at all. (I strongly advise against using other special (and also invisble) space characters.)
Replace the underscore, or the space, vice versa, whenever needed (-> StrReplace()). Eg, if you choose New York from the DDL, just replace all spaces (one) with a _ - then you have the variable name you want to look up: New_York
Thanks! I went with if statements instead of the lists.

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Limit on amount of lists in a script?

Post by gregster » 29 Nov 2021, 22:42

You could simply check with InStr() if the chosen state name contains spaces. If yes, replace them.

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Limit on amount of lists in a script?

Post by jarhead » 29 Nov 2021, 22:45

gregster wrote:
29 Nov 2021, 22:42
You could simply check with InStr() if the chosen state name contains spaces. If yes, replace them.
Great idea... definitely a simple fix. Thanks!

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Limit on amount of lists in a script?

Post by jarhead » 29 Nov 2021, 23:11

Do you know what I'm doing wrong? I tried the following..

Code: Select all

If (State="New Hampshire")
	New_Hampshire := StrReplace(State, A_Space, "_")

GuiControl,, Facility, % "|" %State%
Return
And I replaced my list with

Code: Select all

New_Hampshire := "Manchester"

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Limit on amount of lists in a script?  Topic is solved

Post by gregster » 29 Nov 2021, 23:15

Since you look up State...

Code: Select all

State := StrReplace(State, A_Space, "_")

Code: Select all

New_Hampshire := "Manchester"

State := "New Hampshire"
If (State="New Hampshire")
	State := StrReplace(State, A_Space, "_")
msgbox % "|" %State%
Of course, this is somehow convoluted, if the query is that simple.

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Limit on amount of lists in a script?

Post by jarhead » 29 Nov 2021, 23:18

Thanks man... that did it. I really appreciate the help.

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Limit on amount of lists in a script?

Post by gregster » 29 Nov 2021, 23:22

Now simplify it with Instr(). ;)
Because if you use separate ifs to check for each state with a space, then the generalized StrReplace line is kind of pointless (although not wrong). Because then you could simply do for each state:

Code: Select all

If (State="New Hampshire")
	State := "New_Hampshire"

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Limit on amount of lists in a script?

Post by jarhead » 29 Nov 2021, 23:36

gregster wrote:
29 Nov 2021, 23:22
Now simplify it with Instr(). ;)
Can you provide an example with Instr()? You're right with the multiple if statements but I'm not understanding the use of Instr().

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Limit on amount of lists in a script?

Post by gregster » 29 Nov 2021, 23:47

Code: Select all

New_Hampshire := "Manchester"

; State gets set to something, let's say it's again 
State := "New Hampshire"

If Instr(State, A_Space)
	State := StrReplace(State, A_Space, "_")
msgbox % "|" %State%
Or with a ternary:

Code: Select all

New_Hampshire := "Manchester"

; State gets set, let's say it's again
State := "New Hampshire"

State := Instr(State, A_Space) ? StrReplace(State, A_Space, "_") : State
msgbox % "|" %State%
These checks should replace any spaces in the variable state with underscores... if there is no space, state stays unchanged.

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Limit on amount of lists in a script?

Post by gregster » 29 Nov 2021, 23:53

In this simple case, even the following could already suffice:

Code: Select all

New_Hampshire := "Manchester"

; State gets set to something, let's say it's again 
State := "New Hampshire"

State := StrReplace(State, A_Space, "_") 
msgbox % "|" %State%
If there is no space in it, state gets returned unaltered by StrReplace.

jarhead
Posts: 149
Joined: 09 Sep 2020, 12:43

Re: Limit on amount of lists in a script?

Post by jarhead » 29 Nov 2021, 23:57

Great stuff! Thanks for taking the time to explain and provide the scripts. I've learned a ton from this thread.

Post Reply

Return to “Ask for Help (v1)”