Defaulting Values (on missing object Key_ Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RichardPlester
Posts: 4
Joined: 22 Feb 2021, 13:30

Defaulting Values (on missing object Key_

Post by RichardPlester » 22 Feb 2021, 14:09

Background:
I'm working on code to extract attributes from XML then store them in an assoc. array as key/value pairs for use elsewhere in my code.
The XML is written by others, so may not always contain attributes I expect, therefore I will end up with "missing" keys in my array object,
I'm ok with this as I will just use default values where the XML failed to specify them.

Questions:
1. Is there a preferred "pattern" for defaulting values when a key can't be found in an object? *see code below for my thinking
2. Why does AHK fail so silently in the case of missing keys? Is there anything that can be done about this?

*Is there a clean and readable way of doing the following or is this optimal?

Code: Select all

o := {carmen:"San Diego"}

;Extract some values from object, Default to "not found if key if missing from object
c := o.carmen ? o.carmen : "not found" 
w := o.wally ? o.wally : "not found"

msgbox % "Carmen:"c "`nWally:"w
I'm using AHK 1.1.30.03 (no particular reason) do I have better options in other versions?
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Defaulting Values (on missing object Key_

Post by Hellbent » 22 Feb 2021, 14:24

Can't speak much on this, but you might want to do it this way over the way you have it.

Code: Select all

o := {carmen: 0 }
c := ( o.carmen ) ? ( o.carmen ) : ( "not found" )  ; Will miss the actual value 0

w := {wally: 0 }
d := ( w.wally != "" ) ? ( w.wally ) : ( "not found" ) ;Will catch the 0

msgbox, % "c: " c "`nd: " d
RichardPlester
Posts: 4
Joined: 22 Feb 2021, 13:30

Re: Defaulting Values (on missing object Key_

Post by RichardPlester » 22 Feb 2021, 15:02

Thanks HB, that's an improvement as treating 0 as a "missing" key/value would have been a bug.
RichardPlester
Posts: 4
Joined: 22 Feb 2021, 13:30

Re: Defaulting Values (on missing object Key_

Post by RichardPlester » 22 Feb 2021, 15:50

Further thinking has led me to the following as "" could be an acceptable value too (just like 0 is) - it doesn't mean the key is missing!..

This actually does what I intended, is it optimal though? (definite a DRY issue here at least)

Code: Select all

o := {carmen: 0 } ;..or o := {carmen: ""}
c := o.HasKey("carmen") ?  o.carmen : "not found"  ; Will catch the actual value 0 or "", but use "not found" if key is missing
c := o.HasKey("wally") ?  o.wally : "not found"  ; Will catch the actual value 0 or "", but use "not found" if key is missing
[Mod edit: [code][/code] tags added.]
just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Defaulting Values (on missing object Key_

Post by just me » 23 Feb 2021, 06:11

If you cannot use "" to determine a missing key, o.HasKey() is the only option.
User avatar
Chunjee
Posts: 1433
Joined: 18 Apr 2014, 19:05
Contact:

Re: Defaulting Values (on missing object Key_  Topic is solved

Post by Chunjee » 23 Feb 2021, 14:00

I prefer to set a default object then use a merge to overwrite the keys I do have:

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

defaultObj := {"carmen": "not found", "wally": "not found"}
XML := {"wally": ["123", "456"]}
XML := A.merge(defaultObj, XML)
; => {"carmen": "not found", "wally": [123, 456]}

You can also use https://biga-ahk.github.io/biga.ahk/#/?id=defaults but once a property is set; it won't be overwritten in that call, so kindof reverse order:

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

defaultObj := {"carmen": "not found", "wally": "not found"}
XML := {"wally": ["123", "456"]}
XML := A.defaults(XML, defaultObj)
; => {"carmen": "not found", "wally": [123, 456]}
RichardPlester
Posts: 4
Joined: 22 Feb 2021, 13:30

Re: Defaulting Values (on missing object Key_

Post by RichardPlester » 23 Feb 2021, 16:16

I'll check that library out, I'm parsing SVG elements within the XML file so I can render the SVG using GDI+, the parsing feels hackish in places, hopefully it will become more uniform with biga - thanks.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Defaulting Values (on missing object Key_

Post by swagfag » 23 Feb 2021, 19:42

1. Is there a preferred "pattern" for defaulting values when a key can't be found in an object?
u could create ur own custom associative array class implementing meta-__Get() and meta-__Set() and make it return some default values. the problem is this is hard to implement fully/correctly in v1 due to how the object model discerns between keys and properties not
2. Why does AHK fail so silently in the case of missing keys? Is there anything that can be done about this?
...
I'm using AHK 1.1.30.03 (no particular reason) do I have better options in other versions?
1. has been resolved in v2, so has 2.
u can extend a built-in Map class (the evolution of the associative array) and override its getter/setter to include additional functionality(such as returning default values upon querying nonexistent keys)
the problem is if ure already using v1 specific libraries in ur project, there might not be a v2 equivalent(yet)
Post Reply

Return to “Ask for Help (v1)”