convert list to simple array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

convert list to simple array

26 Jun 2017, 11:32

Code: Select all


z=
(join 
86,85|87,86|88,87|89,88|90,89|91,89|92,90|93,91|94,91|95,92|96,93|97,93|98,94|99,94|100
,94|101,95|102,95|103,95|104,95|105,96|106,96|107,95|108,95|109,95|110,95|111,95|112,95|113
,95|114,94|115,94|116,94|117,93|118,93|119,92|120,92|121,91|122,90|123,90|124,89|125,88|126
,87|127,87|128,86|129,85|130,84|131,83|132,82|133,81|134,81|135,80|136,79|137,78|138,77|139
,76|140,75|141,75|142,74|143,73|144,72|145,72|146,71|147,70|148,70|149,69|150,69|151,68|152
,68|153,67|154,67|155,67|156,66|157,66|158,66|159,66|160,66|161,66|162,66|163,66|164,66|165
,66|166,66|167,67|168,67|169,67|170,68|171,68|172,69|173,69|174,70|175,70|176,71|177,72|178
,72|179,73|180,74|181,74|182,75|183,76|184,77|185,78|186,79|187,79|188,80|189,81|190,82|191
,83|192,84|193,85|194,86|195,86|196,87|197,88|198,89|199,89|200,90|201,91|202,91|203,92|204
,93|205,93|206,94|207,94|208,94|209,95|210,95|211,95|212,95|213,95|214,95|215,95|216,96|217
,96|218,95|219,95|220,95|221,95|222,94|223,94|224,94|225,93|226,93|227,92|228,92|229,91|230
,90|231,90|232,89|233,88|234,87|235,87|236,86|237,85|238,84|239,83|240,82|241,81|242,82
)

pos:={}

a:=strsplit(z,"|")
loop % a.maxindex()
{
b:=strsplit(a[A_Index],",")
pos[b[1]]:=b[2]
}

msgbox % pos[214] 


The list contains x,y values , I want the "y" value when the array key is "x" .The code I could make looks too complicated but I cannot think about a better way :?
If anyone sees a simpler code , I would be grateful !
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: convert list to simple array

26 Jun 2017, 12:20

That looks pretty as much good as possible. So the alternatives I've come up with aren't really better, anyhow:

You can just parse through the pipes in a normal loop. I've avoided creating additional objects, however, I'm using StrSplit twice each time. I'm not sure if there is a performance hit/gain when all the changes are considered.

Code: Select all

q::
z=
(join
86,85|87,86|88,87|89,88|90,89|91,89|92,90|93,91|94,91|95,92|96,93|97,93|98,94|99,94|100
,94|101,95|102,95|103,95|104,95|105,96|106,96|107,95|108,95|109,95|110,95|111,95|112,95|113
,95|114,94|115,94|116,94|117,93|118,93|119,92|120,92|121,91|122,90|123,90|124,89|125,88|126
,87|127,87|128,86|129,85|130,84|131,83|132,82|133,81|134,81|135,80|136,79|137,78|138,77|139
,76|140,75|141,75|142,74|143,73|144,72|145,72|146,71|147,70|148,70|149,69|150,69|151,68|152
,68|153,67|154,67|155,67|156,66|157,66|158,66|159,66|160,66|161,66|162,66|163,66|164,66|165
,66|166,66|167,67|168,67|169,67|170,68|171,68|172,69|173,69|174,70|175,70|176,71|177,72|178
,72|179,73|180,74|181,74|182,75|183,76|184,77|185,78|186,79|187,79|188,80|189,81|190,82|191
,83|192,84|193,85|194,86|195,86|196,87|197,88|198,89|199,89|200,90|201,91|202,91|203,92|204
,93|205,93|206,94|207,94|208,94|209,95|210,95|211,95|212,95|213,95|214,95|215,95|216,96|217
,96|218,95|219,95|220,95|221,95|222,94|223,94|224,94|225,93|226,93|227,92|228,92|229,91|230
,90|231,90|232,89|233,88|234,87|235,87|236,86|237,85|238,84|239,83|240,82|241,81|242,82
)

pos:={}
Loop, Parse, z, |
	pos[StrSplit(A_LoopField, ",")[1]] := StrSplit(A_LoopField, ",")[2]
msgbox % pos[214] ;95
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: convert list to simple array

26 Jun 2017, 12:53

Thanks ,the code looks cleaner that way :)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: convert list to simple array

26 Jun 2017, 13:04

pos:=Object(StrSplit(StrReplace(z,"|",","),",")*)
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: convert list to simple array

26 Jun 2017, 13:08

Helgef wrote:pos:=Object(StrSplit(StrReplace(z,"|",","),",")*)
:bravo:

Based on your solution:

Code: Select all

pos:=Object(StrSplit(z, ["|", ","])*)
my scripts
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: convert list to simple array

26 Jun 2017, 13:24

Very good, I never remember that you can pass an array of delimiters :thumbup:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: convert list to simple array

26 Jun 2017, 13:45

Some good code here. I thought I'd just mention:

Code: Select all

pos:={}
Loop, Parse, z, % "|,"
	(A_Index & 1) ?	(vTemp := A_LoopField) : (pos[vTemp] := A_LoopField)
msgbox % pos[214] ;95
return
Which can be extended, via Mod, to handle triplets/quadruplets cf. just pairs.

==================================================

Code: Select all

q::
vList := "a,b,c|d,e,f|g,h,i"
oArray := {}, vTemp := "", vNum := 3
Loop, Parse, vList, % "|,"
{
	if (Mod(A_Index, vNum) = 1)
		vKey := A_LoopField
	else if (Mod(A_Index, vNum) = 0)
		oArray[vKey] := vTemp A_LoopField, vTemp := ""
	else
		vTemp .= A_LoopField
}
MsgBox, % oArray["a"] " " oArray["d"] " " oArray["g"]
oArray := {}

vList := "a,b,c,d|e,f,g,h|i,j,k,l"
oArray := {}, vTemp := "", vNum := 4
Loop, Parse, vList, % "|,"
{
	if (Mod(A_Index, vNum) = 1)
		vKey := A_LoopField
	else if (Mod(A_Index, vNum) = 0)
		oArray[vKey] := vTemp A_LoopField, vTemp := ""
	else
		vTemp .= A_LoopField
}
MsgBox, % oArray["a"] " " oArray["e"] " " oArray["i"]
oArray := {}
return
Last edited by jeeswg on 26 Jun 2017, 13:53, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: convert list to simple array

26 Jun 2017, 13:52

Creative minds coming together gives an amazing result !

BTW I do not see how it works :shock:

Can you point to the helpfile for the meaning of *) in the object ?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: convert list to simple array

26 Jun 2017, 13:56

To explain how the asterisk works:

Code: Select all

q:: ;'a star is born'
oArray := Object("a", "b", "c", "d")
MsgBox, % oArray["a"] " " oArray["c"]

;===============

oTemp := ["a", "b", "c", "d"]
oArray := Object(oTemp*)
MsgBox, % oArray["a"] " " oArray["c"]

oTemp := Object(["a", "b", "c", "d"]*)
MsgBox, % oArray["a"] " " oArray["c"]

;===============

oTemp := StrSplit("a,b,c,d", ",")
oArray := Object(oTemp*)
MsgBox, % oArray["a"] " " oArray["c"]

oTemp := Object(StrSplit("a,b,c,d", ",")*)
MsgBox, % oArray["a"] " " oArray["c"]

;===============

oTemp := StrSplit("a,b|c,d", ["|",","])
oArray := Object(oTemp*)
MsgBox, % oArray["a"] " " oArray["c"]

oTemp := Object(StrSplit("a,b|c,d", ["|",","])*)
MsgBox, % oArray["a"] " " oArray["c"]

;===============

oArray := oTemp := ""
return
Functions
https://autohotkey.com/docs/Functions.htm#Variadic
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: convert list to simple array

27 Jun 2017, 00:45

Now I see , I missed that it is passing it to the function and not to the "object" so I did not recognize it as variadic code.
Thanks jeeswg

Very nice solution Helgef , something to remember!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: convert list to simple array

30 Jun 2017, 14:26

I adapted this cheeky idea for a 'StrSplit to key names (with blank values)' script:

StrSplit normally puts the substrings as values in a linear array. This puts the substrings as key names in an associative array, each key having a blank value.

You can then use HasKey, to check if a certain line/item existed in the original string.

Code: Select all

q:: ;objects: text to values / text to keys (StrSplit (to values) v. 'StrSplit to key names')
FileRead, vText, % A_ScriptFullPath
vText := "a`r`nb`r`nc`r`nd`r`ne"
Loop, 2
{
	if (A_Index = 1)
		oArray := StrSplit(vText, "`n", "`r")
	else
	{
		oTemp := StrSplit(vText, ["`r","`n"])
		if (oTemp.Length() & 1)
			oTemp.Push("")
		oArray := Object(oTemp*)
		oTemp := ""
	}

	vOutput := ""
	for vKey, vValue in oArray
	{
		vOutput .= vKey " " vValue "`r`n"
		if (A_Index = 10)
			break
	}
	MsgBox, % SubStr(vOutput, 1, -2)
}

;==============================

vText := "f,g,h,i,j"
Loop, 2
{
	if (A_Index = 1)
		oArray := StrSplit(vText, ",")
	else
	{
		oTemp := StrSplit(StrReplace(vText, ",", ",,"), ",")
		if (oTemp.Length() & 1)
			oTemp.Push("")
		oArray := Object(oTemp*)
		oTemp := ""
	}

	vOutput := ""
	for vKey, vValue in oArray
	{
		vOutput .= vKey " " vValue "`r`n"
		if (A_Index = 10)
			break
	}
	MsgBox, % SubStr(vOutput, 1, -2)
}

oArray := oTemp := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: convert list to simple array

30 Jun 2017, 15:03

You can apend a comma to the string before the split,

Code: Select all

Object(StrSplit(StrReplace(keys, ",", ",,") . ",", ",")*)
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: convert list to simple array

30 Jun 2017, 22:39

Wow, I always using {key: "value"}, but didn't know you can use Object("key", "value").. Well, I have seen code like Object("key", "value"), but I thought it equals to ["key", "value"] :lol: . Thanks Helgef.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: convert list to simple array

06 Jul 2017, 22:06

I'm trying to figure out the limits for this technique, in my main script it's crashing at exactly 104082 keys, however, when I launch the code below as a separate script, it can do 104082 keys fine.

Code: Select all

q::
vNum := 100000 ;works
vNum := 104081 ;works
;vNum := 104082 ;causes crash
;vNum := 200000 ;causes crash
vText := ""
VarSetCapacity(vText, 1000000*2)
Loop, % vNum
	vText .= A_Index ",,"
vText := SubStr(vText, 1, -1)
oTemp := StrSplit(vText, ",")
MsgBox, % Strlen(vText) " " oTemp.Length() ;721542 208162
oArray := Object(StrSplit(vText, ",")*)
MsgBox, % oArray.Length() ;104081
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 208 guests