How to StringSplit multiple lines and assign variable for strings? (Help) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

How to StringSplit multiple lines and assign variable for strings? (Help)

10 Mar 2018, 19:46

Hey. I Would like to read the variable fileData's lines one by one, and at the same time split the current string and assign to variables. The strings in each line are in this format xxx:yyy
I was able to string split the very first line and assign it to variables, but for some reason the script doesn't move to the next line and do the same.
help would be appreciated.
Here is my code:

Code: Select all

fileData =
(
horse:1234
dog:9912
cat:8136
bear:0075
)

F1::

lineIndex := 1

    Loop, Parse, fileData, `n
    {
        if (A_Index == lineIndex) {
		Details = %A_LoopField%
		StringSplit, UserArray, Details, `:
		Loop, %UserArray0%
			{
				Content := UserArray%A_Index%
				MsgBox,  Number %A_Index% is %Content%.
			}
            lineIndex++
            break
        }
    }
Return
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

10 Mar 2018, 19:52

Your break after lineIndex++ is snapping you out of the Parsing Loop.

What exactly are you trying to accomplish with the lineIndex anyhow?
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

10 Mar 2018, 20:04

The break lineIndex++ are there because I want it to move to the next line only after pressing F1 key. I am pretty sure that is not the problem tho
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: How to StringSplit multiple lines and assign variable for strings? (Help)  Topic is solved

10 Mar 2018, 20:24

Never mind. I figured where I made a mistake.
Instead of placing lineIndex := 1 before the hotkey F1, I placed it after the hotkey inside the hotkey script, which I shouldn't have done.
It works like a charm now.
here is the code:

Code: Select all

fileData =
(
horse:1234
dog:9912
cat:8136
bear:0075
)

lineIndex := 1

F1::

    Loop, Parse, fileData, `n
    {
        if (A_Index == lineIndex) {
		Details = %A_LoopField%
		StringSplit, UserArray, Details, `:
		Loop, %UserArray0%
			{
				Content := UserArray%A_Index%
				MsgBox,  Number %A_Index% is %Content%.
			}
            lineIndex++
            break
        }
    }
Return
Was a silly mistake from me, but it works like charm now. :)
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

10 Mar 2018, 21:57

Ah, I see. To offer a different perspective, although I'm not sure which is better (I have a hunch mine is because it avoids repeating parsing loops)

Code: Select all

fileData =
(
horse:1234
dog:9912
cat:8136
bear:0075
)
StringSplit, myArray, fileData, `n, `r ; split on the `n, omit any `r in string
return

F1::
counter++
; If (counter>myArray0)
; counter:=1 ; to restart it in a loop if desired
StringSplit, UserArray, myArray%counter%, :
Loop %UserArray0%
   MsgBox % "Number " A_Index " is " (Content:=UserArray%A_Index%) ; just wrapping this into a single-line using expression mode
return
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

11 Mar 2018, 16:24

That works too, and the code seems much more refined. Interesting
garry
Posts: 3772
Joined: 22 Dec 2013, 12:50

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

12 Mar 2018, 03:39

maybe I'm wrong , it seems userarray0 is only the defined row
how can I keep each row in a variable , to use it later ?

Code: Select all

fileData =
(
horse:1234
dog:9912
cat:8136
bear:0075
)
row:=2
Loop, Parse, fileData, `n,`r
{
if (A_Index=row)
  {
  StringSplit,C,a_loopfield, `:
  msgbox,%c1%:%c2%
  }
}
exitapp
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

12 Mar 2018, 04:57

Another way:

Code: Select all

fileData =
(
horse:1234
dog:9912
cat:8136
bear:0075
)
Arr := Object(StrSplit(fileData, ["`n",":"])*)
for key, val in Arr {
	MsgBox % "Key:`t" key "`nVal:`t" val
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
garry
Posts: 3772
Joined: 22 Dec 2013, 12:50

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

12 Mar 2018, 05:30

@Odlanir , thank you
just don't know how can I ask later for variable example arr3= cat:8136

Code: Select all

fileData =
(
horse:1234
dog:9912
cat:8136
bear:0075
)
Arr := Object(StrSplit(fileData, ["`n",":"])*)
for key, val in Arr
   aac .=  key . "-" . val . "`r`n"
msgbox,%aac%
exitapp
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

12 Mar 2018, 05:55

Maybe:

Code: Select all

fileData =
(
horse:1234
dog:9912
cat:8136
bear:0075
)
Arr := []
loop, parse, fileData, `n,`r 
{
	tmpArr := Object(StrSplit(A_LoopField, [":"])*)
	Arr[a_index] := tmpArr
}	
for key, val in Arr[3]
	MsgBox % "Key`t" key "`nVal`t" val
OR:

Code: Select all

Arr := []
loop, parse, fileData, `n,`r 
{
	tmpArr := StrSplit(A_LoopField, [":"])
	Arr[a_index] := {KEY:tmpArr[1],VAL:tmpArr[2]}
}	
MsgBox % "Key`t" Arr[3].key "`nVal`t" Arr[3].val
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

12 Mar 2018, 06:20

@garry : Since in my first example the value pairs key and value are automatically sorted by key name you loose the position of the data ( I mean you can't have access at the third row anymore ) for mantaining the order as you ask you can use the loop to split the data. Hope it make sense.
In my last post I gave you two possible ways to accomplish this.
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
garry
Posts: 3772
Joined: 22 Dec 2013, 12:50

Re: How to StringSplit multiple lines and assign variable for strings? (Help)

12 Mar 2018, 07:02

@Odlanir, thank you very much , works fine
I must learn how to use arrays , was interesting for your last example

Code: Select all

rowx=3
fileData =
(
horse:1234
dog:9912
cat:8136
bear:0075
)

Arr := []
loop, parse, fileData, `n,`r
{
	cx := StrSplit(A_LoopField, [":"])
	Arr[a_index] := {KEY:cx[1],VAL:cx[2]}
}
wanted := Arr[rowx].key . "-" Arr[rowx].val
msgbox,ROW-%rowx%=%wanted%
exitapp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS and 222 guests