text stringlengths 0 897 |
|---|
The keys in a map must be unique; in the table above, you can see that "Buffy", "Willow", "Xander", and "Oz" are all unique. The values, however do not need to be unique; both "Buffy" and "Xander" map to 17 in this example, and that's fine. |
In MiniScript, you can create a map by enclosing it in curly braces, with the key/value pairs separated by commas like in a list. Within each pair, put a colon between the key and value, like this: |
```miniscript |
ages = {"Buffy":17, "Willow":16, "Xander":17, "Oz":18} |
``` |
This defines a map called `ages` containing four key/value pairs. Try it! Type the above assignment into your command-line REPL. Then type |
```miniscript |
ages |
``` |
to see what `ages` contains. You'll get back a representation of the map — but it may not be in exactly the same order you defined it in. It might, for example, look like this: |
```repl |
{"Buffy": 17, "Xander": 17, "Oz": 18, "Willow": 16} |
``` |
A> Maps pay no attention to the *order* of their key/value pairs. Never depend on getting them out in any particular order. |
This is different from a list, which preserve the order of their items. So what good are maps then? They're good for looking up (or storing) values by key. In the same REPL as the above, try this: |
```miniscript |
ages["Oz"] |
``` |
This should print `18`. Notice that the syntax is similar to getting an element of a list or map, except instead of putting a numeric index in the square brackets, we're giving the key (index value) that we want to look up. |
And while you can't tell from such a simple example, it turns out that maps are *really really good* at this sort of look-up. It basically doesn't matter how many items you have stored in a map; a lookup like this will be just as fast. And this too is different from a list, where the average time to find an element d... |
Now let's suppose Willow has a birthday. Let's update her age to 17: |
```miniscript |
ages["Willow"] = 17 |
ages |
``` |
You should see that the value associated with the key "Willow" has been updated. You can add new key/value pairs to the map in exactly the same way: |
```miniscript |
ages["Cordelia"] = 18 |
ages |
``` |
Now the map contains five key/value pairs. Assignment to a map entry will just update the value if the given key is already in the map; otherwise, it will add a new key/value pair. |
Keys and values can be any data type. Try another example: |
```miniscript |
d = {} |
d[1] = "uno" |
d["life"] = "grand" |
d["primes"] = [1,2,3,5,7] |
d |
``` |
The first line above creates an empty map, that is, a map containing no key/value pairs at all. Then we added three keys, one at a time: a number (1) and two strings ("life" and "primes"). The values here include both strings and a list; and of course you previously saw an example where the values were numbers. A va... |
## Square Brackets vs. Dot Syntax |
So far we have assigned and looked up values by putting the key in square brackets, just like indexing into a list or string. But with maps, there is an alternate syntax that will turn out to be really handy. It applies only in the special case where the key is (1) a string, and (2) a valid identifier — meaning that ... |
In this case, you can just put the key *without quotation marks* after the map reference, separated by a dot. If you still have `ages` and `d` defined in your REPL from the examples above, then try this: |
```terminal |
> ages.Oz |
18 |
> ages["Oz"] |
18 |
> d["life"] = "sweet" |
> d.life = "sweeter" |
> d |
{"life": "sweeter", 1: "uno", "primes": [1, 2, 3, 5, 7]} |
``` |
The first two commands do exactly the same thing: look up the value of the key "Oz" in map `ages`. Lines 5 and 6 each assign a new value to the key "life" in map `d`. So the dot syntax is just another way of storing and retrieving values in a map. |
So why is that useful? In a very small program, you can get away without being very organized about your data, because there just isn't very much of it. But as your programs get bigger, it would get really tricky to come up with meaningful, unique variable names for all the values you need to handle, and to write cod... |
(Maps are also the basis for object-oriented programming in MiniScript, but that's such a big topic, we'll spend a whole chapter on it a couple days from now.) |
## Iterating Over Maps |
You already know to write a `for` loop to iterate over elements of a list, or characters in a string. It turns out you can do the same thing to iterate over key/value pairs in a map. When you do, the loop variable is set to little mini-maps containing "key" and "value" entries. An example will make it clearer: |
```miniscript |
ages = {"Buffy":17, "Willow":16, "Xander":17, "Oz":18} |
for kv in ages |
print "I see the key " + kv.key + " goes with value " + kv.value |
end for |
``` |
Try it! You can skip the first line above if you still have `ages` defined in your REPL, and it's OK if your values are a little different from mine. The important thing is to see what the for loop does: it sets the loop variable, `kv`, to each of the key/value pairs in turn. And you access that data in `kv` using `... |
Of course, you could have called the loop variable anything; it doesn't have to be "kv". Here's different example: |
```miniscript |
nums = {"one":"uno", "two":"dos", "three":"tres"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.