KoLmafia supports complex data structures such as maps and records made from simple data types.
If you are new to programming or find the information below confusing, you may want to read A Noob's Guide to Maps first.
Most of this information was copied directly from ASH Maps Tutorial, by Veracity (http://kolmafia.sourceforge.net/advanced.html#maps)
A map is indexed by one data type (the key) and associates that key with another (or the same) data type (the value). The key can be any ASH simple data type: boolean, int, float, string, item, location, class, stat, skill, effect, familiar, slot, or monster. The value can be any ASH data type at all: a simple type, a record, or can be another map. This effectively allows multi-dimensional maps and. In fact, that's how the syntax we provide for multi-dimensional maps actually operate: maps of maps of maps ...
You can declare a map any time you can declare a variable: as a top level (global) variable, as a function parameter, or as a local variable in any scope.
You can fetch data from a map any time you can provide a data value: in an expression, as a function parameter, on the right side of an assignment statement, from a "return" statement, as so on. You can pass around entire maps, individual elements, or intermediate maps: "slices".
The syntax for declaring the data type of a map:
<data type> [ <key type>, ... ] <aggregate_name>
If you use a map on the left side of an assignment, you set the whole map at once to the new value.
You can initialize an entire aggregate with an aggregate literal. For example:
If you specify a map and a complete set of indices (of the correct types) on the left side of an assignment statement, you set a single element.
If you specify a map and a prefix of indices (of the correct type), you directly set one of the intermediate maps, a "slice".
The syntax for referencing an element (or slice) of a map:
<aggregate name>[ <key expression>, ... ]
All the key expressions will be evaluated at run time. If you specify all the keys the map expects, you fetch data of the type specified by the map. If you specify fewer keys than the map expects, you get an intermediate map, a "slice".
As an example:
might be used to hold "properties" associated with names.
props[ "dog", "mammal"] => true boolean [string] animal = props[ "turtle" ]; animal[ "fun" ] => false
You can test the presence of a key in a map using the "contains" operator:
<aggregate reference expression> contains <key expression>
Where <aggregate reference expression> must evaluate at run time to a map or slice, and must evaluate at run time to a key of the appropriate type. (Note that that is enforced at parse time; ASH can tell the datatype any expression will produce).
props contains "dog" => true props contains "elephant" => false props[ "aardvark" ] contains "fun" => true animal contains "pet" => true animal contains "favorite food" => false
You can remove a key-value association from a map using the "remove" unary operator:
remove <aggregate reference>
For clarification, an aggregate reference is "<map name>[ <index 1> ... <index n> ]" where <map name>[ <index 1> ... <index n-1> ] specifies the "slice" and <index n> specifies the "key". Which is just what you expect, if you fully specify the indices; for a single dimensional map, "map" -> "map" is the slice and 10 is the key. The "remove" operator removes the "key" from the "slice". For example:
1 true foo remove: foo 0 false remove: 1 true 17 remove: 17 0 false 0 remove: 0 1 aggregate int [string] remove: aggregate int [string] 0 aggregate int [string]
You can remove all
key => value entries from a map using the clear() function:
The count() function returns the number of defined keys for the specified aggregate.
The syntax is:
aggregate is a reference to the object to be sorted - arrays are probably the most useful things to sort, but any mapping type can be used. But please note that when you sort a map, you change the values that correspond to the index. To sort on a map, you would want to use a multidimensional maps, but note that you can only sort along a single dimension at a time when doing this. Simply put... "sort" is only useful in cases where your data exists entirely in the values of the map; the keys can have no meaning beyond simply being distinct.
The reference must not be enclosed in parentheses, as that would look like a call to a function named
sort() - which is still perfectly valid, "sort" has not become a reserved word.
keyExpr is an arbitrary expression that defines how the items should be ordered. It is evaluated once for every entry in the aggregate, in a scope with two additional variables implicitly defined: '
index' and '
value', holding the details of that entry. The value of the
keyExpr is used as the sort key; typically it would be an
string, but can be any ASH type that can be compared via "<" and the other relational operators.
The most basic form of sorting would therefore be "
sort ... by value", but many useful things can be done with the use of a more complex
keyExpr - the only real restriction is that the expression should not modify the object you're sorting. For example, if you had an array of items, you could sort it "
by autosell_price(value)". An array of weapon items could be sorted "
by -get_power(value)" to put it in decreasing order of power. If the elements of your aggregate are records, you'd need to use something like "
by value.fieldName", since the records themselves can't be meaningfully compared.
After the sort statement, the aggregate will have exactly the same sets of keys and values as before (even if the keys weren't consecutive), and the iteration order of the keys will be the same, but the values will likely be associated with different keys. The sort is stable - in other words, elements with sort keys that compare as equal will remain in the same order. This means that you can sort on multiple criteria by simply performing separate sorts for each of the criteria, in increasing order of significance.
To find out how many things you have, you might do:
Note that this use of an optional feature of foreach. The second variable in the foreach is the value of whatGot[x].
A few more examples of things you can do:
by -value" sorts integers in decreasing order (there's no similar trick for
by -index" reverses the existing order of an array (or map with integer keys).
by random(1000000)" shuffles into a random order.
by otherArray[index]" uses values from a parallel array as the sort keys (you'd then need to do "
sort otherArray by value;" if you wanted the two arrays to remain in sync).
To iterate through a map, use the foreach operator. For instance, if you wanted to print out how many of each item you had, you could do something like the following:
Multidimensional maps are implemented as maps that map keys to maps. int[item][string]map is really a mapping of items to int[string] maps. Iteration, therefore, is as follows:
Two things to note: First, int[item][string]map is equivalent to int[item, string]map. This really comes down to author preference, although the second form is generally more common. Second, the two following foreach loops are equivalent:
Of course, the latter does not lend itself to, say, only printing the first key once, whereas the former can be used that way (see the preceding example).
Maps in ASH are implemented internally as TreeMaps . See below for some implications.
These look and behave like mappings of integers to values, where the keys only take values from 0 to n, but these are implemented as Java Arrays.
Differences between arrays and maps
item  array;
Can use keys 0 - 11. You get a runtime error if you use any other key. It always uses memory to hold 12 items, even if you only use a couple of them. But it's a constant time - O(1) - to access any element.
item [int] map;
Can use any int as a key. It has constant memory for the Java map, and additional memory for each element in the map, but is O( log n) to access any particular element.
If you are able to use (a fairly densely packed set of) integers as keys, your program will be faster and use (potentially) slightly more memory.
If you have a sparse set of integers, you can still use an array and get fast access, but you will waste a lot of memory.
If you can't use integers as keys or don't want to waste memory on a sparse array, you can have a slower but less memory consuming map.
- Given if (a == item1 || a == item2 || a == item3) and if ($items[item1, item2, item3] contains a), which is faster?
This is going to depend on the number of items in the list, and which one happens to match; if 'a' is almost always item1, then the first form is likely to win on practical grounds, even though it's theoretically slower (O(n) vs. O(log n)).
The second form is a definite win assuming no such coincidences of the item chosen, a somewhat larger set of items, and that the code is executed more than once per run of the script. The first lookup in a plural constant actually builds an internal map that allows such queries to be efficiently done; this is deferred because typical use of a plural constant involves only iteration, not lookups.
There's always the "profile" command, if you really need to know which is more efficient in a given situation - although it's unlikely that either would have a noticeable effect on your script's performance.
(copy-pasted from Veracity's post introducing the record )
Starting with SVN revision 1311 of KoLmafia, ASH now supports a new kind of structured data: the record. Here is a little example of how you declare a record and variables of the new type you've created by doing so.
What I've done with the above is declare a new data type which I've named "my_type". Having declared the new type, I can use it (almost) anywhere that I can use a built-in type name. I declared a variable, "rvar", of that type, and I defined a map, "mrvar", which maps keys of type integer to values of type my_type.
The new type, "my_type" is a "composite" type. It contains four fields. "ifield" is an integer. "sfield" is a string. "rfield" is another composite field: an anonymous record containing two integers named "first" and "second". Finally, "mfield" is a map from [int, int] to int.
As you can see, a record can combine data of all the types ASH supports: primitive, aggregate, and composite.
Having defined the new data type and several variables using it, here are some examples of how to access the fields.
As you can see, if you have a variable that is a record, you access the fields of the record by following the variable name with ".<field name>". The resulting value will be of whatever type you declared in the definition of the record. If the value is a map, you can give a list of keys within , just like any other map. If the value is another record, you can access the fields of the nested record by using another ".<field name>".
If you are familiar with Pascal "records" or C/C++ "structs", this should all be comfortably familiar.
Finally, if you create a map whose values is a record, the file_to_map and map_to_file built-in ASH functions will Do The Right Thing; they will efficiently and reliably save and restore your data.