Jump to content

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

creating an associative array of associative arrays


  • Please log in to reply
5 replies to this topic
Benny-D
  • Members
  • 1485 posts
  • Last active: Sep 27 2016 11:23 PM
  • Joined: 29 Feb 2008

Let's say I have the following table:

 

 

             profession      height      country        city
Jack         teacher         tall        USA            New-York
Paul         cook            short       UK             London
Bill         designer        short       Canada         Toronto   
Max          driver          tall        USA            Dallas
Sam          policeman       tall        Australia      Canberra 

 

I want to turn this whole table into an associative array. How can I do it?

 

I know how to turn each line of that table into an associative array. The third line, for example, I would do in this way:  

 

Bill := {}
Bill.profession := "designer"
Bill.height := "short"
Bill.country := "Canada"
Bill.city := "Toronto"
For key, value in Bill
   MsgBox, %key%   ==>   %value% 

, but how can I turn the whole table into an associative array? I guess it should be an array of arrays, but can I do that?



Linear Spoon
  • Members
  • 842 posts
  • Last active: Sep 29 2015 03:56 AM
  • Joined: 29 Oct 2011

Do you mean you want to read that info in from a file or that you want to hard code it? An example:

 

people := {}

;Either of these should work
people["Bill"] := Bill
people.Paul := Paul

 

 

Bill := {}
Bill.profession := "designer"
Bill.height := "short"
Bill.country := "Canada"
Bill.city := "Toronto"


people := {}
people.Bill := Bill

;accessing elements in this array:
For key, value in people.Bill
   MsgBox, %key%   ==>   %value% 

Join us at the new forum - http://www.ahkscript.org/

 


Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012
Jack := {profession: "teacher"
         , height: "tall"
         , country: "USA"
         , city: "New York"}

Paul := {profession: "cook"
         , height: "short"
         , country: "UK"
         , city: "London"}

Bill := {profession: "designer"
         , height: "short"
         , country: "Canada"
         , city: "Toronto"}

Max := {profession: "driver"
        , height: "tall"
        , country: "USA"
        , city: "Dallas"}

Bill := {profession: "policeman"
         , height: "tall"
         , country: "Australia"
         , city: "Canberra"}

Person := {Jack: Jack
           , Paul: Paul
           , Bill: Bill
           , Max: Max
           , Bill: Bill}


MsgBox, % Person.Jack.city


Benny-D
  • Members
  • 1485 posts
  • Last active: Sep 27 2016 11:23 PM
  • Joined: 29 Feb 2008

Thank you Linear Spoon!!! I wanted exactly what you showed me here in these examples. What does it mean to "hard code it"?

 

Benny-D
  • Members
  • 1485 posts
  • Last active: Sep 27 2016 11:23 PM
  • Joined: 29 Feb 2008

Coco thank you!!!



Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012

You're welcome. AHK's object syntax is like JSON, So when creating nested associative arrays, you can write it JSON-like for a much better overview of each level. That way you don't get confused or lost and you can organize it better.wink.png