[Bro-Dev] set and vector operators
Azoff, Justin S
jazoff at illinois.edu
Wed Apr 25 10:54:31 PDT 2018
> On Apr 25, 2018, at 1:40 PM, Vern Paxson <vern at icir.org> wrote:
>
> I'm working on some scripts that use sets and vectors, sometimes together,
> and am finding it clunky that Bro doesn't offer much in the way of operators
> for this. To that end, I'm thinking of implementing some along the following
> lines, where values starting with 's' are sets, 'v' are vectors, and 'e'
> are type-compatible elements:
>
> s1 + s2 Set union (for sets of the same type, of course)
> s1 || s2 Set union
>
> s1 && s2 Set intersection
> s1 - s2 Set difference
>
> s += e Add the element 'e' to set 's'
> (same as the current "add s[e]")
> s -= e Remove the 'e' element from 's', if present
> (same as the current "delete s[e]")
>
> s1 += s2 Same as "s1 = s1 + s2"
> s1 -= s2 Same as "s1 = s1 - s2"
>
> v += e Append the element 'e' to the vector 'v'
> v += s Append the elements of 's' to the vector 'v',
> with the order not being defined
>
> s += v Add the elements of 'v' to 's'
> s -= v Remove the elements of 'v' from 's', if present
>
> These strike me as pretty straightforward, but please chime in if you
> have comments!
That's very similar to what python does, except they use & and | instead of && and ||.
I think they do that because 'set or' is closer to 'bitwise or' than 'logical or'
They also use ^ for symmetric difference.
>>> a=set([1,2,3])
>>> b=set([2,3,4])
>>> a & b
{2, 3}
>>> a | b
{1, 2, 3, 4}
>>> a - b
{1}
>>> b - a
{4}
>>> a ^ b
{1, 4}
—
Justin Azoff
More information about the bro-dev
mailing list