Page 1 of 1

How to add maps?

Posted: 22 Apr 2021, 19:31
by iseahound

Code: Select all

map1 := map(
   "a","apple",
   "b","banana",
   "c","clementine"
)

map2 := map(
   "d","dragonfruit",
   "e","elderberry",
   "f","fig"
)

map1.set(map2)

MsgBox map1['f']
It's not clear how a map can be added to the other.

Re: How to add maps?

Posted: 23 Apr 2021, 08:32
by jNizM
Do you want to extend map 2 into map 1?
than this is one way (but it also overwrites existing keys)

Code: Select all

for k, v in map2
	map1.set(k, v)

MsgBox map1["f"]

Re: How to add maps?

Posted: 23 Apr 2021, 09:00
by CptRootBeard
If you don't want to overwrite existing keys, you just need to use the Has() method.

Code: Select all

;;Only set the value if map1 doesn't have the key
for k, v in map2{
	if !map1.Has(k) {
		map1.set(k, v)
	}
}

MsgBox(map1["f"])

Re: How to add maps?

Posted: 23 Apr 2021, 09:13
by kczx3
Yeah this is a weird question. It seems like you're more asking for operators to work on Maps. Like Map1 + Map2 or something which doesn't exist. @CptRootBeard has the right solution I'd say.

Re: How to add maps?

Posted: 01 May 2021, 16:54
by swagfag
why are u using .Set() for individual key value pairs? set only makes sense if ure about to insert a bunch at a time
if its solely to avoid calling __Item, a better(ie faster) approach would be to coalesce whatever items u need inserted into an array first, then spread the array to .Set(theArray*) them all at once
of course, doing that comes at the expense of memory, but if things got to the point where is a problem, using the original approach would have become a problem much much sooner than that