Another approach would be to use ^query as Bruce suggested and to use facts and create actual key/value pairs.
In an effort to make facts more accessible I am posting a couple of output macros that wrap some of the fact logic in more familiar commands .
The following implement “mapadd” to create a key value pair and “map” to retrieve a value based on the key:
# Add Key Value Pair to a Map
outputmacro: ^mapadd( ^mapname ^key ^value )
$$mapname = ^join( mapname: ^mapname )
^createfact( $$mapname ^key ^value )
# Retrieve Key Value Pair from a Map
outputmacro: ^map( ^mapname ^key )
$$mapname = ^join( mapname: ^mapname )
@0 = ^query( direct_sv $$mapname ^key ? 1 )
@0object
You can place the following in a topic to add and retrieve key/value pairs interactively:
# Add a Key Value Pair to a Map
#! mapadd( daysofweek , 1 , Monday )
u: ( mapadd \( _* , _* , _* \) ) ^keep ^repeat
^mapadd( ‘_0 ‘_1 ‘_2 )
Key\/Value pair \( ‘_1 , ‘_2 \) was added to map \” ‘_0 \”.
# Retrieve a Value for a Key in a Map
#! map ( daysofweek , 1 )
u: ( map \( _* , _* \) ) ^keep ^repeat
$$value = ^map( ‘_0 ‘_1 )
Key \” ‘_1 \” has a value of \” $$value \” in map \” ‘_0 \”.
Sample Output:
>mapadd( daysofweek , 7 , Sunday )
Key/Value pair (7, Sunday ) was added to map “daysofweek”.
>map( daysofweek , 7 )
Key “7” has a value of “Sunday” in map “daysofweek”.
>
The command names could just as easily been named “factadd” and “getfactobject”.
Note: The fact subject is added with “mapname:” prefixed to the actual subject so that other facts do not mix together with these “maps” or named sets of key/value pairs. If you wanted to query for the mapname in the facts you will need to prefix “mapname:” to the subject being queried.