Next: , Previous: Declaring Tables, Up: Tables



3.12.2 Initializing Tables

You initialize tables by enclosing a set of initializers within braces. Each initializer looks like:

[ expr-list ] = expr
where expr-list is a comma-separated list of expressions corresponding to an index of the table (so, for a table indexed by count, for example, this would be a single expression of type count) and expr is the yield value to assign to that index.

For example,

         global a: table[count] of string = {
             [11] = "eleven",
             [5] = "five",
         };

initializes the table a to have two elements, one indexed by 11 and yielding the string "eleven" and the other indexed by 5 and yielding the string "five". (Note the comma after the last list element; it is optional, similar to how C allows final commas in declarations.)

You can also group together a set of indices together to initialize them to the same value:

         type HostType: enum { DeskTop, Server, Router };
         global a: table[addr] of HostType = {
             [[155.26.27.2, 155.26.27.8, 155.26.27.44]] = Server,
         };

is equivalent to:

         type HostType: enum { DeskTop, Server, Router };
         global a: table[addr] of HostType = {
             [155.26.27.2] = Server,
             [155.26.27.8] = Server,
             [155.26.27.44] = Server,
         };

This mechanism also applies to which can be used in table initializations for any indices of type addr. For example, if www.my-server.com corresponded to the addresses 155.26.27.2 and 155.26.27.44, then the above could be written:

         global a: table[addr] of HostType = {
             [[www.my-server.com, 155.26.27.8]] = Server,
         };

and if it corresponded to all there, then:

         global a: table[addr] of HostType = {
             [www.my-server.com] = Server,
         };

You can also use multiple index groupings across different indices:

         global access_allowed: table[addr, port] of bool = {
             [www.my-server.com, [21/tcp, 80/tcp]] = T,
         };

is equivalent to:

         global access_allowed: table[addr, port] of bool = {
             [155.26.27.2, 21/tcp] = T,
             [155.26.27.2, 80/tcp] = T,
             [155.26.27.8, 21/tcp] = T,
             [155.26.27.8, 80/tcp] = T,
             [155.26.27.44, 21/tcp] = T,
             [155.26.27.44, 80/tcp] = T,
         };

Fixme: add example of cross-product initialization of sets