Iteration over vectors.

This commit is contained in:
Scott Runnels 2013-01-08 15:31:56 -05:00
parent 7d583b6b78
commit c453c228cb
3 changed files with 27 additions and 4 deletions

View file

@ -196,8 +196,6 @@ The table below shows the common data types used in Bro, of which, the first fou
| pattern | regular expression |
+-----------+-------------------------------------+
Data Structures
---------------
Vectors
~~~~~~~
@ -209,13 +207,23 @@ The format for the declaration of a Vector follows the pattern of other declarat
.. literalinclude:: ../../../../testing/btest/doc/manual/data_struct_vector_declaration.bro
:language: bro
:linenos:
:lines: 6,7
:lines: 4-16
.. btest:: data_type_const.bro
@TEST-EXEC: btest-rst-cmd bro -b ${TESTBASE}/doc/manual/data_struct_vector_declaration.bro
In a lot of cases, storing elements in a vector is simply a precursor to then iterating over them.
In a lot of cases, storing elements in a vector is simply a precursor to then iterating over them. Iterating over a vector is easy with the for keyword. The sample below iterates over a vector of IP addresses and for each IP address, masks that address with 18 bits.
.. literalinclude:: ../../../../testing/btest/doc/manual/data_struct_vector_iter.bro
:language: bro
:linenos:
:lines: 4-12
.. btest:: data_type_const.bro
@TEST-EXEC: btest-rst-cmd bro -b ${TESTBASE}/doc/manual/data_struct_vector_iter.bro
Sets
~~~~

View file

@ -0,0 +1,3 @@
1.2.0.0/18
2.3.0.0/18
3.4.0.0/18

View file

@ -0,0 +1,12 @@
# @TEST-EXEC: bro %INPUT
# @TEST-EXEC: btest-diff .stdout
event bro_init()
{
local addr_vector: vector of addr = vector( 1.2.3.4, 2.3.4.5, 3.4.5.6);
for (i in addr_vector)
{
print mask_addr(addr_vector[i], 18);
}
}