diff --git a/doc/user-manual/scripting.rst b/doc/user-manual/scripting.rst index 09d1a1ef8d..fcb812ce20 100644 --- a/doc/user-manual/scripting.rst +++ b/doc/user-manual/scripting.rst @@ -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 ~~~~ diff --git a/testing/btest/Baseline/doc.manual.data_struct_vector_iter/.stdout b/testing/btest/Baseline/doc.manual.data_struct_vector_iter/.stdout new file mode 100644 index 0000000000..0326e6580e --- /dev/null +++ b/testing/btest/Baseline/doc.manual.data_struct_vector_iter/.stdout @@ -0,0 +1,3 @@ +1.2.0.0/18 +2.3.0.0/18 +3.4.0.0/18 diff --git a/testing/btest/doc/manual/data_struct_vector_iter.bro b/testing/btest/doc/manual/data_struct_vector_iter.bro new file mode 100644 index 0000000000..96574eab1b --- /dev/null +++ b/testing/btest/doc/manual/data_struct_vector_iter.bro @@ -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); + } + }