Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

AutoHotkey_L v1.1.00


  • Please log in to reply
17 replies to this topic
guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
from the docs
<!-- m -->https://ahknet.autoh... ... ive_Arrays<!-- m -->

Using the {key:value} notation, quote marks are optional for keys which consist only of word characters. Any expression can be used as a key, but to use a variable as a key, it must be enclosed in parentheses. For example,

{(KeyVar): Value}


i'm curious why a variable is de-referenced using parentheses instead of the usual %% signs?

and also, why do variables as VALUES not need to be de-referenced at all?

i guess, more generally, why the difference between key and value notation? why not allow keys to have the same (imo more natural) notation as values?

myvar := "3"
one := "oonnee"

array := {1:one, 2:"two", (myvar):"three"}

For key, value in array
    MsgBox %key% = %value%


here, variable 'one' doesnt need to be de-referenced because its used as a VALUE, but variable 'myvar' does since its a KEY

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

Should this be listed under objects in the the Help File?

Sure. I've mostly neglected the help file table of contents, if that's what you're talking about.

i'm curious why a variable is de-referenced using parentheses instead of the usual %% signs?

Any key which doesn't strictly follow the rules for a variable name is treated as an expression. (var) is an expression which yields a variable. GetMyKey() and prefix . suffix are equally valid.

and also, why do variables as VALUES not need to be de-referenced at all?

The premise of your question is flawed: {key: value} does dereference value. There's nothing special about the value in a key-value pair - it's interpreted like any other part of the expression.

i guess, more generally, why the difference between key and value notation? why not allow keys to have the same (imo more natural) notation as values?

Literal key names are far more common than variable ones. Do you want to use quotation marks around every key name? It's perfectly legal to do so, but generally inconvenient and less readable, IMO.

why not allow keys to have the same (imo more natural) notation as values?

They do; the only exception is that if it looks like just a variable name, it's treated as a key name instead.

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
thanks that clears it up.

regarding higher order functions, it seems that AHK was already able to do this with dynamic function calling, but heres an example for those who didn't realize (like myself):

myarray := [1,2,3,4,5]

old_map("square", myarray)
enum(myarray)

squareRef := Func("square")
new_map(squareRef, myarray)
enum(myarray)

square(int) {
   sq := int * int
   return sq
}

old_map(func, array) {
   for key, value in array
      array[key] := %func%(array[key])
}

new_map(funcRef, array) {
   for key, value in array
      array[key] := funcRef.(array[key])
}

enum(obj) {
   for key, value in obj
      MsgBox %key% = %value%
}