AutoHotkey Community

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

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: September 10th, 2011, 2:35 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
I would like to see some kind of syntax to elegantly reorder an array structure. Example:
Code:
array := [{value1 : 1, value2 : 2}, {value1 : 3, value2 : 4}]
for index, value in array[].value1 ;Loop over all value1 keys
    msgbox % value


The above syntax is just a suggestion that might not even work. It would probably be easily possible to do this as a function but I think an integrated syntax might be better to use.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2011, 4:13 pm 
Offline
User avatar

Joined: November 2nd, 2008, 4:23 pm
Posts: 2906
Location: 127.0.0.1
Here's an example way to do it with a function:
Code:
array := [{value1 : 1, value2 : 2}, {value1 : 3, value2 : 4}]
for index, value in GetKeys(array, "value1") ;Loop over all value1 keys
   msgbox % value


GetKeys(obj, keyname, depth=1) {
   out := []
   for k,v in obj
      if (k = keyname)
         out.Insert(v)
      else if IsObject(v) && depth >= 1
         for k2, v2 in GetKeys(v, keyname, depth-1)
            out.Insert(v2)
   return out
}


Or you could do it with an if statement,
Code:
array := [{value1 : 1, value2 : 2}, {value1 : 3, value2 : 4}]
for index, value in array ;Loop over all value1 keys
   if array.haskey("value1")
      msgbox % value.value1


If there were a built-in way to do this, it could be a where clause.
Code:
array := [{value1 : 1, value2 : 2}, {value1 : 3, value2 : 4}]
for index, value in array where value.haskey("value1")
   msgbox % value.value1
The last part would be a Boolean value. If it's false, continue to the next item. It would have access to the key and value variables as you define them, and anything in the parent scope.

_________________
aboutscriptappsscripts
Any code ⇈ above ⇈ requires AutoHotkey_L to run


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2011, 11:45 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
Nice implementations. I didn't mean to limit it to loops only though, it can also be useful for parameter passing to functions for example.

GetKeys will include a possibly existing key with the same name from the main object though.

Some kind of where operator would be a nice thing to have, it adds some more style to loops ;). It might even be possible to implement it as a generic operator that could be used outside of loops on objects. Syntax might need to be extended to something like
Code:
larger := key.value from array where key.value > 5


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You can 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