appending to a vector

Hi,

What's the recommended way to append to a vector? The documentation says vectors are like tables, so I tried the below code, but it gives some errors.

const foo: vector of double = vector() &redef;

redef foo += {
     [|foo|] = 42.0
};

print(foo);

Hi David,

I think you’re looking for something like this:

const foo: vector of double = vector() &redef;
foo[|foo|] = 42.0;

print(foo);

v/r
Scott

That works... but there's no redef. I thought normal assignment to a const variable was supposed to fail. Isn't that a bug?

What's the recommended way to append to a vector?

Many (most?) of us strongly dislike vectors in Bro. It's likely something that will be improved in the next couple of releases if I had to make a guess. :slight_smile:

The documentation
says vectors are like tables, so I tried the below code, but it gives
some errors.

const foo: vector of double = vector() &redef;

redef foo += {
    [|foo|] = 42.0
};

Hm, I don't know about this if you add more than a single element at a time because the length wouldn't be right.

If you are using our git master you can use a new construct that was recently added for creating types containers.

type DoubleVector: vector of double;
const foo = DoubleVector([0] = 42.0, [1] = 43.0, [2] = 44.0);

Generally though you're going to run into weird poorly defined edge cases like this with vectors, when they were added to Bro it was in a sort of ad-hoc fashion and not fully thought out except for the use case in which they were originally used. (the code I gave above may not even work, I haven't tested it!)

  .Seth

Yeah, that's probably a bug.

  .Seth

What's the recommended way to append to a vector?

Many (most?) of us strongly dislike vectors in Bro. It's likely
something that will be improved in the next couple of releases if I
had to make a guess. :slight_smile:

Maybe there's a better way to do what I want without vectors. I have a somewhat complex record type and I want to store multiple records of that type in a container of some sort. I want to be able to iterate over the container, but I don't care about order (vector) or uniqueness (set). When I tried using sets, I got the error below, so I switched to vectors.

internal error: over-ran key in CompositeHash::RecoverVals
Aborted

Generally though you're going to run into weird poorly defined edge
cases like this with vectors

:slight_smile:


What did your set code look like?

I suppose you had a type like "type MySet: set[MyRecord];" defined?

  .Seth

In that case, does it need to be a constant?

Modifying the set/vector/collection after bro_init() would have no effect and could cause quite a bit of confusion. Const is a useful way to express that.

What's the recommended way to append to a vector? The documentation
says vectors are like tables, so I tried the below code, but it gives
some errors.

const foo: vector of double = vector() &redef;

redef foo += {
    [|foo|] = 42.0
};

Appending to a vector can't currently be done w/ redef, but I don't think it would be difficult to implement if you want to add a ticket to the tracker.

Maybe there's a better way to do what I want without vectors. I have a
somewhat complex record type and I want to store multiple records of
that type in a container of some sort. I want to be able to iterate over
the container, but I don't care about order (vector) or uniqueness
(set). When I tried using sets, I got the error below, so I switched to
vectors.

internal error: over-ran key in CompositeHash::RecoverVals
Aborted

Can you give an example script that reproduces that (create a bug/ticket for it) ?

I thought normal assignment to a const variable was supposed to fail. Isn't that a bug?

I agree that it shouldn't be allowed, but just to explain how it's currently working: the const applies to the variable, not the value bound it. So you can't assign further values to the const variable, but its value may still be mutable depending on its type.

- Jon

What's the recommended way to append to a vector? The documentation
says vectors are like tables, so I tried the below code, but it gives
some errors.

const foo: vector of double = vector() &redef;

redef foo += {
    [|foo|] = 42.0
};

Appending to a vector can't currently be done w/ redef, but I don't
think it would be difficult to implement if you want to add a ticket
to the tracker.

I got number 1024 :smiley:

http://tracker.bro.org/bro/ticket/1024

Maybe there's a better way to do what I want without vectors. I have a
somewhat complex record type and I want to store multiple records of
that type in a container of some sort. I want to be able to iterate over
the container, but I don't care about order (vector) or uniqueness
(set). When I tried using sets, I got the error below, so I switched to
vectors.

internal error: over-ran key in CompositeHash::RecoverVals
Aborted

Can you give an example script that reproduces that (create a
bug/ticket for it) ?

If I can reproduce it, I'll file a bug.

I thought normal assignment to a const variable was supposed to fail. Isn't that a bug?

I agree that it shouldn't be allowed, but just to explain how it's
currently working: the const applies to the variable, not the value
bound it. So you can't assign further values to the const variable,
but its value may still be mutable depending on its type.

Gotcha.