# script question

**URL:** <https://community.zeek.org/t/script-question/4477>\
**Category:** Zeek\
**Created:** [November 8, 2016, 9:52pm UTC](https://community.zeek.org/t/script-question/4477 "2016-11-08T21:52:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dk\_Jack](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@Dk\_Jack](https://community.zeek.org/u/Dk_Jack)\
**Post date:** [November 8, 2016, 9:52pm UTC](https://community.zeek.org/t/script-question/4477/1 "2016-11-08T21:52:29Z")

</div>

Hi,  
Found an interesting quirk of bro scripting. Not sure if its a quirk or a bug.

function myfunction()  
{  
if (T) {  
local var = “Hello, World!”;  
print var;  
} else {  
local var = “Goodbye, World!”;  
print var;  
}  
}

For this code, I get the error:

```auto
error in ././trybro.bro, line 6 and ././trybro.bro, line 9: already defined (var)

```

```auto
If I make the local variable names different i.e. var and var2, it doesn't complain. I think this is a bug. Let me know...

```

```auto
Dk.

```

```auto
PS: Since I couldn't find an ends_with function, I wrote one. Let me know if this is ok...

```

```auto

```

```auto
function ends_with(input_string: string, match_pattern: string) : bool
{
  local offset = |input_string| - |match_pattern| + 1;
  local sstring = sub_bytes(input_string, offset, |match_pattern|);

  if (sstring == match_pattern)
    return T;
  else
    return F;
}

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Vern](https://yyz1.discourse-cdn.com/flex011/user_avatar/community.zeek.org/vern/32/630_2.png) [@Vern](https://community.zeek.org/u/Vern)\
**Post date:** [November 9, 2016, 7:09am UTC](https://community.zeek.org/t/script-question/4477/2 "2016-11-09T07:09:03Z")

</div>

> Found an interesting quirk of bro scripting. Not sure if its a quirk or a  
> bug.

It's a quirk actually, by which I mean known behavior since I implemented  
local variables way back 'n the day. The philosophical view may have been  
that it's confusing to have the same variable name mean different things  
inside the same function, so good to avoid that; but I might have done it  
simply for implementation convenience, I don't recall which at this point.

Given that it's surprising behavior, and arguably not particularly beneficial,  
I wouldn't mind changing it.

&nbsp;&nbsp;&nbsp;&nbsp;Vern

---

<div class="post-metadata">

**Author:** ![Dk\_Jack](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@Dk\_Jack](https://community.zeek.org/u/Dk_Jack)\
**Post date:** [November 9, 2016, 8:05pm UTC](https://community.zeek.org/t/script-question/4477/3 "2016-11-09T20:05:12Z")

</div>

yeah, was surprised a little since it goes against the scoping rules of most languages I’ve dealt with scripting or otherwise…  
Perhaps an update to documentation would be helpful…

Bhasker.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/zeek/original/1X/f09d732bc2cc7c7cc7e35db67cf4e1d5233ce7a7.png) [@system](https://community.zeek.org/u/system)\
**Post date:** [May 6, 2022, 3:44pm UTC](https://community.zeek.org/t/script-question/4477/4 "2022-05-06T15:44:16Z")

</div>


