How to handle - StringSplit? Topic is solved

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

How to handle - StringSplit?

18 Dec 2018, 17:36

What's the best way to handle the result from StringSplit?
An example!
First MsgBox shows that StringSplit works as desired.
The second MsgBox shows that StringSplit has not emptied any indexed variables.

Code: Select all

String = "art_id";"art_name2";"trade_mark";"art_info3"
StringSplit Field_, String, %A_Space%`;`r`n, "
MsgBox 64, Rad %A_LineNumber% -> %A_ScriptName%, % "( " String " )`n`nField_0 .: " Field_0 "`nField_1 .: " Field_1 "`nField_2 .: " Field_2 "`nField_3 .: " Field_3 "`nField_4 .: " Field_4 "`nField_5 .: " Field_5

String = 
StringSplit Field_, String, %A_Space%`;`r`n, "
MsgBox 64, Rad %A_LineNumber% -> %A_ScriptName%, % "( " String " )`n`nField_0 .: " Field_0 "`nField_1 .: " Field_1 "`nField_2 .: " Field_2 "`nField_3 .: " Field_3 "`nField_4 .: " Field_4 "`nField_5 .: " Field_5
My wish was that in the second MsgBox all indexed variables would have been emptied.
(I don't know if the input string have 0, 2, 5 or 72 fields)

Right now, maybe the only solution is to empty the variables with a Loop before StringSplit

Code: Select all

Loop 2
{	Loop % Field_0	; Clear Indexed fields!
		Field_%A_Index% =
	String = "art_id";"art_name2";"trade_mark";"art_info3"
	StringSplit Field_, String, %A_Space%`;`r`n, "
	MsgBox 64, Rad %A_LineNumber% -> %A_ScriptName%, % "( " String " )`n`nField_0 .: " Field_0 "`nField_1 .: " Field_1 "`nField_2 .: " Field_2 "`nField_3 .: " Field_3 "`nField_4 .: " Field_4 "`nField_5 .: " Field_5
	
	Loop % Field_0	; Clear Indexed fields!
		Field_%A_Index% =
	String = 
	StringSplit Field_, String, %A_Space%`;`r`n, "
	MsgBox 64, Rad %A_LineNumber% -> %A_ScriptName%, % "( " String " )`n`nField_0 .: " Field_0 "`nField_1 .: " Field_1 "`nField_2 .: " Field_2 "`nField_3 .: " Field_3 "`nField_4 .: " Field_4 "`nField_5 .: " Field_5
}
ExitApp

ESC::
	ExitApp
Return
Maybe there are better solutions?
Would other instructions work better than StringSplit in my case?
(I think about, if Object would be a better solution, but at the moment, I think the structure would be more complicated.)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to handle - StringSplit?

18 Dec 2018, 18:02

what even is the point of discussion here? other than "im stuck with a 10yo ahk interpreter", there is absolutely no reason to use StringSplit. its a deprecated command, so do as the docs suggest and use StrSplit() instead.
u can then easily clear all ur split strings in 1 line of code, by assigning blank to ur object:

Code: Select all

mySplitStrings := StrSplit("a|b|c", "|")
mySplitStrings := ""
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: How to handle - StringSplit?

19 Dec 2018, 05:09

Thanks!
(Had missed that difference between these two commands)
But if I want to know how many fields in the string?

With the command StringSplit it is easy, but with StrSplit.......
(I have no idea - maybe count delimiters(;) or use StingSplit to that)
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: How to handle - StringSplit?

19 Dec 2018, 05:23

Albireo wrote: count delimiters(;)

Code: Select all

MsgBox % StrSplit("a;b;c", ";").Count()

Code: Select all

v := StrSplit("a;b;c", ";")
MsgBox % v.count()
My Scripts and Functions: V1  V2
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: How to handle - StringSplit?

19 Dec 2018, 06:46

SKAN wrote:
19 Dec 2018, 05:23
Albireo wrote: count delimiters(;)

Code: Select all

MsgBox % StrSplit("a;b;c", ";").Count()

Code: Select all

v := StrSplit("a;b;c", ";")
MsgBox % v.count()
Have tested both examples above, but the answers is blank.
I think this will work in most cases .:

Code: Select all

String := "a;b;c"
v := StrSplit(String, ";")
MsgBox % "- " v[1] "`n- " v[2] "`n- " v[3] "`nCount .: " v._MaxIndex()
But this give me an empty result (not "0") .:

Code: Select all

String = 
v := StrSplit(String, ";")
MsgBox % "- " v[1] "`n- " v[2] "`n- " v[3] "`nCount .: " v._MaxIndex()
Has not analyzed if it causes problems in other instructions, that the variable is ""(empty) and not "0"(zero)
(for example in if-, loop- instructions and so on)
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: How to handle - StringSplit?

19 Dec 2018, 06:59

Count() requires v1.1.29
What version are you using?
My Scripts and Functions: V1  V2
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: How to handle - StringSplit?  Topic is solved

19 Dec 2018, 08:24

it gaves zero which is ok

Code: Select all

String:= ""
v := StrSplit(String, ";")
total:=v.count()
msgbox,Total=%total%
return
examples , also with stringsplit

Code: Select all

dlm:=",$,"
txt2:="aaa1" . dlm . "bbb1" . dlm . "ccc1"
;----------------
ct:=strsplit(txt2,dlm).Count()
c :=strsplit(txt2,dlm)
c1:=c[1]
c2:=c[2]
c3:=c[3]
total:=c.count()
msgbox,Total=%total% or %ct% `nC1=%c1%`nC2=%c2%`nC3=%c3% 
;---------------------
;---------------------
txt1:="aaa2,bbb2,ccc2"   ;- only 1 delimiter 
;------------
stringsplit,h,txt1,`,
msgbox,Total=%h0%`nH1=%h1%`nH2=%h2%`nH3=%h3% 
return
;------------------
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: How to handle - StringSplit?

19 Dec 2018, 13:40

Thank you!
Now it works!
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: How to handle - StringSplit?

19 Dec 2018, 13:56

it was SKAN's script , just combined
@SKAN , thank you , glad to meet you more often again here
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: How to handle - StringSplit?

19 Dec 2018, 14:19

garry wrote:glad to meet you more often again here
:thumbup:
My Scripts and Functions: V1  V2

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 233 guests