Split string only if left part contains an element of z

Get help with using AutoHotkey and its commands and hotkeys
EmmaEmily
Posts: 26
Joined: 25 Sep 2019, 05:30

Split string only if left part contains an element of z

05 Apr 2020, 08:01

Hallo guys,


I want to split my string strExample1 only if the characters to the right of 1080

contain an element of z.

Code: Select all

; Example strings:

strExample1 := "Split string after 1080 only if Don element of z"   ; split 
;strExample1 := "Split string only if 1080 something Elton like!"    ; don't split

z = Jane|Mike|Eva|Elle|Jo|Don

varA = false
Loop parse, z, |
If (InStr(strSplit(strExample1, "1080")[2], A_LoopField))	
varA = True

If varA = True
strString := strSplit(strExample, "1080")[1]
else
strString := strExample1

; further code to do something with strString ...
Is there a better way to do this?
Thanks for your help and advice!
User avatar
boiler
Posts: 4561
Joined: 21 Dec 2014, 02:44

Re: Split string only if left part contains an element of z

05 Apr 2020, 08:23

Your version only works if it matches the last item in z. You need to break the loop after it finds it to avoid that. Also, everything you have with varA being True and False is just adding lines to do what if already does anyway. Take that stuff out and just make it like this:

Code: Select all

strExample1 := "Split string after 1080 only if Jo element of z"
z = Jane|Mike|Eva|Elle|Jo|Don

Loop parse, z, |
{
	If (InStr(strSplit(strExample1, "1080")[2], A_LoopField))
	{
		strString := strSplit(strExample1, "1080")[1]
		break
	}
	else
		strString := strExample1
}
MsgBox, % strString
Edit: I see that your version didn't work only because you left out a "1" in one of your strExample1. I see why you used the varA variable now.
User avatar
BNOLI
Posts: 527
Joined: 23 Mar 2020, 03:55

Re: Split string only if left part contains an element of z

05 Apr 2020, 08:41

I've put it into a function, just for the fun of it. :)

Code: Select all

nameIndex = Jane|Mike|Eva|Elle|Jo|Don   ; index of names to parse

!a::MsgBox % TruthSeeker("Split string after 1080 only if Don element of z", nameIndex)    ; function call (press ALT+a to trigger function) and display returned result.
!b::MsgBox % TruthSeeker("Split string only if 1080 something Elton like!", nameIndex)      ; function call (press ALT+b to trigger function) and display returned result.

TruthSeeker(string, nameIndex) {    ; function that is processing the content of the function call, using two parameters.
   Loop parse, NameIndex, |    ; parsing the nameIndex 
        Response := if InStr(strSplit(string, "1080")[2],A_LoopField) ? "True" : "False"    ; split string, validate 2nd field for existing name, assign bolean statement to response var.
   Return % Response   ; return result
}
edit: added comments to the code
Last edited by BNOLI on 06 Apr 2020, 04:48, edited 2 times in total.
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
EmmaEmily
Posts: 26
Joined: 25 Sep 2019, 05:30

Re: Split string only if left part contains an element of z

05 Apr 2020, 10:44

@BNOLI
Unfortunately your code does not work for me and I do not know why? Can you please help me?
User avatar
BNOLI
Posts: 527
Joined: 23 Mar 2020, 03:55

Re: Split string only if left part contains an element of z

06 Apr 2020, 04:32

You know that I've used Hotkeys to trigger a MsgBox for a demonstration? (I'll add some comments to the script for further advise)

Start the script. It will stay in idle mode.

Press Alt+a to get the validation result of the first string.
Press Alt+b to get the validation result of the second string.
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, Bral, congdantoancau, F4Jonatas, Google [Bot], Kellyzkorner_NJ, malcev, ThumpieBunnyEve, wyatterp and 90 guests