How to distinguish numeric map key vs array index? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

How to distinguish numeric map key vs array index?

Post by RaptorX » 26 Sep 2022, 16:08

Hi. Using [Beta.9].

Im trying to access a map key that happens to be a numeric id. Thing is that if I pass it as a variable AHK thinks im trying to access an array index which is incorrect.

How do I force it behave as a map key.

Code: Select all

id := contacts[1]["id"]
invoices := invoices["list"][id] ; This fails
invoices := invoices["list"]["722399"] ; This works
Projects:
AHK-ToolKit

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: How to distinguish numeric map key vs array index?  Topic is solved

Post by RaptorX » 26 Sep 2022, 16:15

Well i just forced the variable to behave as a string by appending some quotes like this:

Code: Select all

invoices := invoices["list"][id ""] ; this now works
Projects:
AHK-ToolKit

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to distinguish numeric map key vs array index?

Post by lexikos » 26 Sep 2022, 19:30

Thing is that if I pass it as a variable AHK thinks im trying to access an array index which is incorrect.
This is not v1. A Map does not behave as an Array. A Map is a Map. There are only integer, string and object keys, not array indices.

A variable does not behave as a string. A variable contains a string or something else.

Maps are type-sensitive. Apparently your variable contained a number, not a string. You didn't force it to behave as a map key. You converted the numeric key to a string key, so instead of corresponding to nothing, it corresponds to the value previously associated with that string.

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: How to distinguish numeric map key vs array index?

Post by RaptorX » 27 Sep 2022, 09:44

When I said "I just made the variable behave as a string" I meant the value that the var contains. I was not specific enough. But yeah we are talking about the same thing.
lexikos wrote:
26 Sep 2022, 19:30
Apparently your variable contained a number, not a string.
Yeah and as I have no control of how that data is coming (comes from an API Call) I didnt see any problems with passing it directly to my map.
But as it turns out I do need to do some conversion prior to passing it to my map object.
lexikos wrote:
26 Sep 2022, 19:30
Thing is that if I pass it as a variable AHK thinks im trying to access an array index which is incorrect.
Maps are type-sensitive. Apparently your variable contained a number, not a string. You didn't force it to behave as a map key. You converted the numeric key to a string key, so instead of corresponding to nothing, it corresponds to the value previously associated with that string.
Yes, this is exactly what happened.

I think the process of converting a numeric value, passed as a variable to a map, to a string is a bit counterintuitive.
this invoices := invoices["list"][id ""] feels really odd to me. Having no built-in way to convert one type to another in the form of id.toString() for example, is a bit weird. I usually dont like the .toString stuff but is a very common way other languages solve the type conversion issues.

As soon as V2 introduced some form of types, we might need a built in way to cast/convert between them without having to resort to some hacky solutions that for me make sense at the moment, but 4 months from now im gonna see those wierd quotes and probably wont remember why I put them there in the first place :D
Projects:
AHK-ToolKit

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to distinguish numeric map key vs array index?

Post by swagfag » 27 Sep 2022, 12:03

the built-in way is to use the respective constructor, eg String(somethingThatIsHopefullyConvertible)

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: How to distinguish numeric map key vs array index?

Post by RaptorX » 27 Sep 2022, 13:45

swagfag wrote:
27 Sep 2022, 12:03
the built-in way is to use the respective constructor, eg String(somethingThatIsHopefullyConvertible)
oh, was not aware of that, let me check on that.
Projects:
AHK-ToolKit

Post Reply

Return to “Ask for Help (v2)”