Baby Bro, Part 3: Containers and Loops
Bro has four main container types, which I'm going to cover in somewhat nontraditional order:- tables
- sets
- vectors
- records
A table is a collection of indexed key-value pairs: the same idea is referred to as a dictionary, associative array, or hash table in other languages. Here's a simple example that pairs letters with their place in the alphabet:
1 | event bro_init() |
Running it, we get this:
jswan@so12a:~/bro$ bro tables.bro
{
[3] = c,
[1] = a,
[2] = b
}
Note that the output isn't in the same order as the script; in Bro, like in most other languages, hash tables are unordered.
Iterating over a table with a "for" loop returns the key, again like other languages:
1 | event bro_init() |
And the output:
jswan@so12a:~/bro$ bro tables.bro
Continue reading