suve

awful: tutorial: dictionaries

Arrays are quite cool, but they have this one restriction - indexes can only be numbers. Sometimes we might want to store data in a tad different way. Here's where we may find awful's another structure type, the dictionary, quite useful. Dictionaries are almost identical to arrays, except they use strings for indexing.

Santa saw your facebook, you're getting clothes and a dictionary for Christmas

:set &d :dict s'one' s'eins' s'two' s'zwei'
:set &d[s'three'] s'drei'
:set &d[s'four' ] s'vier'
:set &d[s'five' ] s'fünf'

:set &wrd s""
:writeln s'Write a word: '
:read &wrd

:writeln s'English(' $wrd s') == German(' $d[$wrd] s')'

The above example demonstrates how to use dictionaries. As can be seen, they are created using the :dict function. It can take any number of arguments, and processes them in pairs - the left one is used as index, while the right one as value. This means that the dictionary is created with two elements - string eins at index one, and string zwei at index two. Later, three more elements are added by assigning to indexes, just like we've seen done with arrays. At the end, we access the selected index of the dictionary and print it.

Is this all?

Obviously, array and dictionary functionality is not limited only to creating them and accessing their elements. awful contains many functions intended to work with these structures. You can read about them in the arrdict unit documentation. And be sure to read the next chapter of the tutorial - loops and structures go really well together and allow to do amazing things.


Next: ⇒ Loops ⇒
Back: ⇐ Arrays ⇐

wikipage modified on 2014/0601/2317