Special use of Switch / Case - possible Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Special use of Switch / Case - possible

Post by Albireo » 14 Apr 2021, 14:08

My wish with Case / Switch is to handle different files by its filename.
Managing specific file names is no problem.
But all files starting with ö&... should be handled in a special way (z&... in the test program below)
The files should not be handled by Switch / Default

Is it possible?

In the test program below 1 and 2 works as desire, (but not z& or z&123)

Code: Select all

#SingleInstance force
Loop
{	InputBox xInput, Give a value, % "Enter (1 or 2 or z& ...)" ,, 200, 150
	switch xInput
	{	Case "1"		:	Gosub One
		Case "2"		:	Gosub Two
		Case subStr(xInput, 1, 2) = "z&" : Gosub Next
	
		Default	: 	MsgBox % "Wrong input .: " xInput
	}
}
ExitApp

One:
	MsgBox % "One `nxInput .: " xInput
Return

Two:
	MsgBox % "Two `nxInput .: " xInput
Return

Next:
	MsgBox % "Next `nxInput .: " xInput
Return

ESC:: ExitApp

User avatar
boiler
Posts: 16769
Joined: 21 Dec 2014, 02:44

Re: Special use of Switch / Case - possible

Post by boiler » 14 Apr 2021, 14:40

Do you mean something like this? It's not clear what you're saying. You want to use Switch but you don't want the special case handled by Switch? If not like below, you could just use an If statement before the Switch so the special case doesn't go through the Switch.

Code: Select all

#SingleInstance force
Loop
{	InputBox xInput, Give a value, % "Enter (1 or 2 or z& ...)" ,, 200, 150
	switch SubStr(xInput, 1, 2)
	{	Case "1"		:	Gosub One
		Case "2"		:	Gosub Two
		Case "z&"		:	Gosub Next
	}
}
ExitApp

One:
	MsgBox, One
return

Two:
	MsgBox, Two
return

Next:
	MsgBox, Next
return

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Special use of Switch / Case - possible

Post by Albireo » 14 Apr 2021, 18:08

Thank you for the try! (but it doesn't work)
Will try to explain in a different way...
I have many files I want to sort in different directories, partly due to the file name (but also the contents of the files)
Right now I have about 30 different "case" (
case file1 : ...
case file2 : ...
...
case file35 : ...

and so on (no problem) These files must be handled individually.

But it is also 30-40 files as begin with ö&... (z&... in the test prog.)
ö& file1...
ö& file2...
...
ö& file25...

All these files should be handled in the same way.
It is possible to continue with more "case" like this .:
case ö&_file40... : Gosub Next
case ö&_file41... : Gosub Next
....
case ö&_file67... : Gosub Next

However, additional new "case" must be added when new files appear. (which can happens quite often - maybe every week)

Tested this idea - (but did not work either)

Code: Select all

Loop
{	InputBox xInput, Give a value, % "Enter (1 or 2 or 123 or z&&...)" ,, 200, 150
	
	If !( SubStr(xInput, 1, 2) = z& )
	{	Switch % xInput
		{	Case "1"		:	Gosub One
			Case "2"		:	Gosub Two
			Case "123"	:	Gosub Three
			Default		: 	MsgBox Other result
		}
	}
	else
		Gosub Next

}

User avatar
boiler
Posts: 16769
Joined: 21 Dec 2014, 02:44

Re: Special use of Switch / Case - possible  Topic is solved

Post by boiler » 14 Apr 2021, 19:09

Albireo wrote:
14 Apr 2021, 18:08
Thank you for the try! (but it doesn't work)
It does work for the range of possible inputs you described in your first post, which were 1, 2, z& ... In fact, the InputBox prompt in your own code said Enter (1 or 2 or z& ...). Now you say it doesn’t work because it doesn’t handle a bunch of other cases not mentioned before.


One problem I noted in the rest of your post was this line:

Code: Select all

	If !( SubStr(xInput, 1, 2) = z& )
Literal strings in expressions must be enclosed in quotation marks:

Code: Select all

	If !( SubStr(xInput, 1, 2) = "z&" )

That’s about as far as I’m going to try to follow it. Perhaps someone else will try to keep up.

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Special use of Switch / Case - possible

Post by Albireo » 15 Apr 2021, 03:41

Yes! :dance:
Thank you!
Now the test Switch / Case works in this way!

Code: Select all

#SingleInstance force
Loop
{	InputBox xInput, Give a value, % "Enter (1 or 2 or 123 or z&&...)" ,, 200, 150
	
	If !(SubStr(xInput, 1, 2) = "z&")
	{	Switch % xInput
		{	Case "1"		:	Gosub One
			Case "2"		:	Gosub Two
			Case "123"	:	Gosub Three
			Default		: 	MsgBox % xInput " not in Case"
		}
	}
	else
		Gosub Next
}

One:
	MsgBox % "One `nxInput .: " xInput
Return

Two:
	MsgBox % "Two `nxInput .: " xInput
Return

Three:
	MsgBox % "Three `nxInput .: " xInput
Return

allOther:
	MsgBox % "allOther (with z& in the beginning) `nxInput .: " xInput
Return


ESC:: ExitApp

Post Reply

Return to “Ask for Help (v1)”