简体   繁体   中英

What is the equivalent of Java's HashMap in AutoHotkey?

I have a set of abbreviated department names. I need to create a script which can map these abbreviations with their official titles. (For example: ADMIN → Administration)

In Java I could accomplish this using a HashMap .

public static void main() {
   HashMap hm = new HashMap(); // create hash map

   hm.put("ADMIN", "Administration");  // add elements to hashmap
   hm.put("RAD",   "Radiologist");
   hm.put("TECH",  "Technician");

   System.out.println("ADMIN is an abbreviation for " + hm.get("ADMIN"));
}

Is there an equivalent solution for this in AutoHotkey?

You can implement key-value pairs using an Associative Array

An associative array is an object which contains a collection of unique keys and a collection of values, where each key is associated with one value. Keys can be strings, integers or objects, while values can be of any type. An associative array can be created as follows:

 Array := {KeyA: ValueA, KeyB: ValueB, ..., KeyZ: ValueZ} 

Here is an array which uses a job's shortened name ( key ) to find the full display name ( value ).

JobArray := {ADMIN:"Administration", TECH:"Technician", RAD:"Radiologist"}

; Check if key is present
if (JobArray.HasKey("ADMIN"))
    MsgBox, % "ADMIN is an abbreviation for " . JobArray["ADMIN"]
else
    MsgBox, % "No display name found"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM