default function not get called in Table ...

Hi everyone

I have a got a problem. I want to call a function called world but it is throwing the following error:

line 1: error: unknown identifier world, at or near “world”

global a: table[count] of string &default = world;

a[1] = “navdeep”;
a[2] = “singh”;

print a[2];

function world(): string
{
print “hello world”;
}

Please resolve the problem.

Regards
Navdeep

The main problem you're running into here is that you're defining the world function after you're trying to use it. When you use functions as default values, there is an argument passed to the function that is the value of the index and you forgot to return the string value from the world function. Lastly, when you call most code it needs to be inside of an event handler, but you can wrap the code inside of a bro_init event handler which executes once Bro has completed it's initialization.

I'll include a fixed version of your test script.

function world(i: count): string
{
return fmt("hello world - index is %d", i);
}

global a: table[count] of string &default=world;

event bro_init()
         {
         a[1] = "navdeep";
         a[2] = "singh";

         print a[2];
         print a[3];
         }

----script output----
$ ./src/bro test5
singh
hello world - index is 3

   .Seth