How to insert IDs of customers as KEYS and names of customers as VALUES in an Associative Array? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

How to insert IDs of customers as KEYS and names of customers as VALUES in an Associative Array?

07 Jun 2019, 12:31

Hello Friends..


Please look at these codes-

Code: Select all

str=
(
10058	JAMES MARTIEN
920098	SABESTIAN CAINE
6500042	DAVID MAC JONESLEY
)
; The above is a very long list. Above three lines are just a sample..

for key, value in StrSplit(str, "`n")
{
	
	RegExMatch(value, "mi)(\d+)\s(\w.*)", obj)
	MsgBox % obj1 "`n" obj2
}


In the above codes, you can see that str contains a small list of customer ids and customer names. obj1 and obj2 contains the id number and customer name of each field respectively. I want to convert the data of str into associative array and then retrieve the customer id and customer name by for loop. Namely, I want that the key of array should contain the customer id and value of array should contain the name of customer. However, I can solve my purpose using obj1 and obj2, but I want to use key-value pair in associative array.

like this (Imaginary/dummy codes)-

Code: Select all

str=
(
10058	JAMES MARTIEN
920098	SABESTIAN CAINE
6500042	DAVID MAC JONESLEY
)

for key, value in StrSplit(str, "`n")
{
	
	RegExMatch(value, "mi)(\d+)\s(\w.*)", obj)
	Array:= {obj1 : obj2}
}

for key, value in Array
	msgbox % key "`n" value   ; here key should show the customer ID and value should show the name of customer

Please tell me how to do that?

Thanks a lot...
I don't normally code as I don't code normally.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to insert IDs of customers as KEYS and names of customers as VALUES in an Associative Array?

07 Jun 2019, 12:54

Code: Select all

str=
(
10058	JAMES MARTIEN
920098	SABESTIAN CAINE
6500042	DAVID MAC JONESLEY
)

Array := {}
for key, row in StrSplit(str, "`n")
{
	CustomerInfo := StrSplit(row, "`t") ; if ur list is tab-separated, which it seemingly is
	id := CustomerInfo[1]
	name := CustomerInfo[2]
	Array[id] := name
}

for id, name in Array
	msgbox % clipboard := id ": " name
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: How to insert IDs of customers as KEYS and names of customers as VALUES in an Associative Array?  Topic is solved

07 Jun 2019, 12:59

Code: Select all

#SingleInstance, Force

str =
(
10058	JAMES MARTIEN
920098	SABESTIAN CAINE
6500042	DAVID MAC JONESLEY
)

MyArray := {}

Loop, Parse, str, `n
{
	RegExMatch(A_LoopField, "mi)(\d+)\s(\w.*)", obj)
	MyArray[obj1] := obj2	
}

; Using For Loop
For CustID, CustName In MyArray {
	MsgBox, % CustID " - " CustName
}

; Using Customer ID
MsgBox, % MyArray.10058
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to insert IDs of customers as KEYS and names of customers as VALUES in an Associative Array?

07 Jun 2019, 14:15

TheDewd wrote:
07 Jun 2019, 12:59

Code: Select all

#SingleInstance, Force

str =
(
10058	JAMES MARTIEN
920098	SABESTIAN CAINE
6500042	DAVID MAC JONESLEY
)

MyArray := {}

Loop, Parse, str, `n
{
	RegExMatch(A_LoopField, "mi)(\d+)\s(\w.*)", obj)
	MyArray[obj1] := obj2	
}

; Using For Loop
For CustID, CustName In MyArray {
	MsgBox, % CustID " - " CustName
}

; Using Customer ID
MsgBox, % MyArray.10058

Thanks dear TheDewd ... This is what i was wanting...

I was also very close to this. I got by here-

Code: Select all

str=
(
10058	JAMES MARTIEN
920098	SABESTIAN CAINE
6500042	DAVID MAC JONESLEY
)

a:= {}
for key, value in StrSplit(str, "`n")
{
	
	RegExMatch(value, "mi)(\d+)\s(\w.*)", obj)
	a.obj1:= obj2  ; here I was making mistake... i should have written a[obj1]:=obj2
	
}
for key, value in a
	MsgBox % key "`n" value
Anyway, Thanks you so much for your kind help and support.... :thumbup: :thumbup: :thumbup:

Take care...
I don't normally code as I don't code normally.
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to insert IDs of customers as KEYS and names of customers as VALUES in an Associative Array?

07 Jun 2019, 14:16

swagfag wrote:
07 Jun 2019, 12:54

Code: Select all

str=
(
10058	JAMES MARTIEN
920098	SABESTIAN CAINE
6500042	DAVID MAC JONESLEY
)

Array := {}
for key, row in StrSplit(str, "`n")
{
	CustomerInfo := StrSplit(row, "`t") ; if ur list is tab-separated, which it seemingly is
	id := CustomerInfo[1]
	name := CustomerInfo[2]
	Array[id] := name
}

for id, name in Array
	msgbox % clipboard := id ": " name
Thanks dear swagfag.... Your way of converting str to array is also great.... Thank you so much sir... :bravo: :bravo:
I don't normally code as I don't code normally.
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: How to insert IDs of customers as KEYS and names of customers as VALUES in an Associative Array?

08 Jun 2019, 05:52

Or even shorter:

Code: Select all

str=
(
10058	JAMES MARTIEN
920098	SABESTIAN CAINE
6500042	DAVID MAC JONESLEY
)

for id, name in Array := Object(StrSplit(str, ["`n","`t"])*)
	msgbox % clipboard := id ":" name

ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, mikeyww, mmflume, roysubs, scriptor2016, ShatterCoder and 103 guests