AutoHotkey Object-Oriented Notation for Associative Arrays (A Short Intro)

Special Object-Oriented Syntax Makes It Easier to Retrieve Array Data

I hesitate to discuss Object-Oriented Programming (OOP) in AutoHotkey. I haven’t work with it enough to provide the insight I would like when addressing a topic. When reading online tutorials, I have a tough enough time understanding the explanations. I have yet to see a tutorial that makes it simple. So, I concentrate on the pieces that get results right now without going too much into the weeds.

From what I’ve read, OOP acts as the de facto standard for professional programmers—not without controversy (“Object-Oriented Programming Is Bad?“). They say that the planning and organization which comes with using OOP makes life easier for multiple people toiling on large projects. While this approach to programming may work for large projects, it does not necessarily make life easier for short apps such as most AutoHotkey scripts.

Library Benefits

While you can use certain aspects of OOP in AutoHotkey, it’s not required. In version 1.1 of AutoHotkey, true arrays offer the primary form of a programming object. (Version 2.0 adds GUI objects and Menu objects to the list.) These array objects offer a number of advantages over the traditional pseudo-arrays. However, in-depth OOP implementation includes classes and methods which take more time to comprehend and implement.

Rather than a stringent form of OOP, AutoHotkey offers a more flexible prototype-based programming approach without requiring a strict definition of classes and methods. That means you can use bits and pieces of the code as deemed necessary.

For my AutoHotkey scripts, I prefer Solution-Oriented Programming (SOP). By Solution-Oriented Programming, I mean do whatever you find that works without too much complication. In the MouseMeasureMultiLine.ahk script (“Track Graphic Line Measurement Segments Using AutoHotkey Arrays“), associative arrays using object-oriented notation work well to track the legs of a journey.

Object-Based Associative Array Objects

Object-based associative arrays allow the saving of a variety of divergent values in a single array. Those values may or may not relate to each other. As long as you define a key and value (key:value) you can add it to the array. This creates tremendous flexibility when working with variables. But in AutoHotkey, you must first create an object for the associative array. Create the array object with one of the following forms:

People := Array()

or

People := Object()

or

People := {}

or

People := []

Each of the above statements creates the object needed to build the data structure of an associative array. Although not needed in this example, a variation of the third example can create the object with the key:value pairs included:

People := {Rick:Rick,Bob:Bob,Jack:Jack}

This last form of array object creation uses variables for the values allowing us to create an array with each name. (You do not need to predefine any of the arrays or properties. You can add them as you need them.)

After creating the object, the exact form of your data structure depends on how you define it—not the initial creation of the object.

Note: You’ll find that throughout associative array notation a number of different methods for doing the same thing. I don’t think it matters which you use as long as it gets the job done.

Array Properties

People.Rick.Year
Relationship
Bob.Year
Relationship
Jack.Year
Relationship
Birthday.Month
Day
Year

AutoHotkey allows the assignment of properties (or relationships) to the associative array object. This establishes a connection between the object and the values of the properties.

Note: You could build-in relationships between variables without using associative arrays, but you would need either some type of lookup table or assume any relationship (possibly through similar variable names as in pseudo-arrays) rather than immediately defining the association.

When defining a property of an object, that property attaches to the object. For example, the following arrays use the object People to add three names with the properties Year and Relation:

People.Rick := {Year: "1986", Relation: "Friend"}
People.Bob := {Year: "1988", Relation: "Cousin"}
People.Jack := {Year: "1990", Relation: "Brother"}

The names Rick, Bob, and Jack become properties of the object People—each defining an associative array containing the next level properties Year and Relation. Note: This works only if you first define the object People using one of the five object creation examples above.

To recall the specific values of the properties we cascade the variable form inserting dots between each level:

People.Rick.Year       ⇒  "1986"
People.Rick.Relation   ⇒  "Friend"
People.Bob.Year        ⇒  "1988"
People.Bob.Relation    ⇒  "Cousin"
People.Jack.Year       ⇒  "1990"
People.Jack.Relation   ⇒  "Brother"

It’s easy to see how a programmer can immediately access and manipulate data by merely knowing property names (keys) and relationships within the associative array. We can continue to add more arrays and properties:

People.Jack := {Year: "1950", Relation: "Brother", Birthday: Birthday}

Using this approach, we add the property (key) Birthday at the time of the array creation and leave the value as the empty variable Birthday—available for further definition as an array.

As an alternative approach to adding new properties, we can directly define the property as another sub-array without first adding the key:value to the parent array:

People.Jack.Birthday := {Month: "June", Day: 7, Year: 1950}

We can use this form of associative array creation to directly assign the property to the previous property without requiring the original definition.

As an example of recalling the values of an array, the following snippet including a For-Loop using the object notation to add values to a MsgBox command:

People := Array()
People.Rick := {Year: "1946", Relation: "Friend"}
People.Bob := {Year: "1948", Relation: "Cousin"}
People.Jack := {Year: "1950", Relation: "Brother", Birthday: Birthday}
People.Rick.Relation := "Friend"
People.Jack.Birthday := {Month: "June", Day: 7, Year: 1950}

For Key , Value in People.Jack.Birthday
{
	If Key = Month
		Month := Value
	If Key = Day
		Day := Value
	If Key = Year
		Year := Value
}
	Birthday := Month " " Day ", " Year

MsgBox % People.Rick.Year . "`r" . People.Bob.Relation 
	. "`r" People.Jack.Year  . " " . People.Jack.Birthday "`r" 
	. People.Jack.Birthday.Month " " People.Jack.Birthday.Day ", " People.Jack.Birthday.Year
	. " ==> " Birthday


Even though the object People uses a name for each sub-property, the object does not contain the names of the people as properties. For that we require a statement such as:

People.Rick := {First: "Richard, Last: "Smith", Year: "1946", Relation: "Friend"}

This provides a more complete set of data.

Whether or not you use associative arrays and how you design them totally depends upon how you plan to use them. As for digging deeper into Object-Oriented Programming, I’ll leave that to the experts. Until I find a more compelling reason to immerse myself in that style of scripting, I’ll only use the coding techniques when I see obvious benefits—such as with associative arrays.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

2 thoughts on “AutoHotkey Object-Oriented Notation for Associative Arrays (A Short Intro)

  1. Here’s my favorite article about the ‘great divide’ discussing OOP versus Procedural programming.
    I’m an ABAP programmer working with SAP global enterprise planning software.
    There was a gigantic push to get everyone into practicing OOP techniques but it never really got off the ground.
    Here’s an article from one of the best SAP ABAP developers, Paul Hardy that I think you’ll find amusing.
    It ultimately deals with the ABAP language, but the first page or two applies to anyone trying to work with OOP techniques.
    https://blogs.sap.com/2012/10/27/crossing-the-great-divide-procedural-vs-oo-abap-programming/

    Thanks for providing everyone with AutoHotkey. I use it every day.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s