AutoHotkey Community

It is currently May 27th, 2012, 6:40 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: May 5th, 2011, 5:25 am 
Offline

Joined: March 10th, 2011, 7:17 pm
Posts: 374
from the docs
http://www.autohotkey.net/~Lexikos/Auto ... ive_Arrays
Quote:
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,
Code:
{(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?

Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: AutoHotkey_L v1.1.00
PostPosted: May 5th, 2011, 9:00 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
a4u wrote:
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.
guest3456 wrote:
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.
Quote:
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.
Quote:
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.
Quote:
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 5th, 2011, 10:42 pm 
Offline

Joined: March 10th, 2011, 7:17 pm
Posts: 374
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):

Code:

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%
}



Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher and 1 guest


You cannot post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group