Update cloned array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Update cloned array

08 Oct 2020, 13:39

Code: Select all

Bill := {}
Bill["Country"] := "Canada"
Bill["City"] := "Toronto"
Bill["Profession"] := "Designer"

Karl := {}
Karl["Country"] := "UK"
Karl["City"] := "London"
Karl["Profession"] := "Manager"

Jane := {}
Jane["Country"] := "UK"
Jane["City"] := "London"
Jane["Profession"] := "Artist"

Customers := {}
Customers["Bill"] := Bill
Customers["Karl"] := Karl

GetCustomerCity(Customer)
{
    Global Customers
    Customers := Customers.Clone()

    ExistingCustomersCity := Customers[Customer]["City"]
    If (ExistingCustomersCity != "")
        Return ExistingCustomersCity

    NewCustomer := Customers[Customer] := Customer
    Return Customers[NewCustomer]["City"]
}

MsgBox % GetCustomerCity("Bill")  ; => Toronto
MsgBox % GetCustomerCity("Jane")  ; => nothing
How to fix the case of Jane?
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: Update cloned array

08 Oct 2020, 13:46

You are missing this:

Code: Select all

Customers["Jane"] := Jane
Seems like it’s much more complex than it needs to be, btw.

I suppose your function is trying to handle cases where the particular customer hasn’t been added, but I’m not seeing why.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Update cloned array

08 Oct 2020, 13:57

I suppose your function is trying to handle cases where the particular customer hasn’t been added,
Yes, this is correct.
but I’m not seeing why.
You mean, what is the reason? Just curiosity.
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: Update cloned array

08 Oct 2020, 14:00

So you have a use case where you have already created a customer and filled in its information, but you haven't added it to your overall customer list object. And the way you add it is by querying its city? Why wouldn't you just add it to the main object when you create it?
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: Update cloned array

08 Oct 2020, 14:17

One problem is the object Jane doesn't exist in the function. You can't add it just because you've sent it the name of it. There is no object named Jane to reference within the scope of the function.

It's not really a good practice to name the objects by something that should actually be a value/property within that object (the name). Each time you want to add an object, you have to hard code their name in as a new object name?
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: Update cloned array  Topic is solved

08 Oct 2020, 14:31

Not that I would do it this way, but this is closer to what you are looking to do:

Code: Select all

Bill := {}
Bill["Name"] := "Bill"
Bill["Country"] := "Canada"
Bill["City"] := "Toronto"
Bill["Profession"] := "Designer"

Karl := {}
Karl["Name"] := "Karl"
Karl["Country"] := "UK"
Karl["City"] := "London"
Karl["Profession"] := "Manager"

Jane := {}
Jane["Name"] := "Jane"
Jane["Country"] := "UK"
Jane["City"] := "London"
Jane["Profession"] := "Artist"

Customers := {}
Customers["Bill"] := Bill
Customers["Karl"] := Karl

MsgBox % GetCustomerCity(Bill)  ; => Toronto
MsgBox % GetCustomerCity(Jane)  ; => nothing
return

GetCustomerCity(Customer)
{
    Global Customers
    
	if !Customers[Customer["Name"]]
		Customers[Customer["Name"]] := Customer

    Return Customers[Customer["Name"]]["City"]
}
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Update cloned array

08 Oct 2020, 21:21

Good job boiler. I was looking at the original function and thinking it could be done more simply.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Hugh Jars, Mateusz53, MrDoge, peter_ahk and 371 guests