# Using variable in function call

**URL:** <https://community.gatling.io/t/using-variable-in-function-call/2382>\
**Category:** Gatling (Open-Source)\
**Created:** [June 24, 2015, 11:06am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382 "2015-06-24T11:06:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukas\_lang](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lukas\_lang](https://community.gatling.io/u/lukas_lang)\
**Post date:** [June 24, 2015, 11:06am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/1 "2015-06-24T11:06:23Z")

</div>

Hi all,  
i have a problem when i want to call function with variables from session as parameters:

`

.check(regex(""“data-page=”(\d).2" data-question""").findAll.saveAs(“list1”))  
.check(regex(""“data-page=”(\d).2" data-question""").count.saveAs(“count1”))

`

which found (2, 5).

Then i have this function “spustanie” which calls another function “footer”

`

def spustanie(var\_list:List[String],var\_count:Int)={

for (i ← 0 to var\_count-1) {  
var lst2 = var\_list(i).toString  
var re2 = lst2  
if (i == var\_count-1) re2  
else{  
re2 = (lst2.toInt + 1).toString  
}

footer (lst2+",2",re2)  
}  
}

`

I have tried something like

`

.exec(session =\> {  
spustanie(session(“list1”).as[List[String]],session(“count1”).as[Int])  
session  
})

`

which just doesnt seem to work.

Thanks in advance for any help. 🙂

---

<div class="post-metadata">

**Author:** ![lukas\_lang](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lukas\_lang](https://community.gatling.io/u/lukas_lang)\
**Post date:** [June 24, 2015, 11:10am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/2 "2015-06-24T11:10:22Z")

</div>

Sorry i forgot, im using Gatling 2.1.6

---

<div class="post-metadata">

**Author:** ![Andrew\_Krischer](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Andrew\_Krischer](https://community.gatling.io/u/Andrew_Krischer)\
**Post date:** [June 25, 2015, 4:56pm UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/3 "2015-06-25T16:56:45Z")

</div>

Hm, it’s not clear to me what your problem is. What are you expecting to happen and what did happen?

From what I can see, I’m guessing that you mean **session(“list1”).as[List[String]]** is not returning a List[String]?  
Looking at the documentation, [http://gatling.io/docs/2.1.6/http/http\_check.html?highlight=findall](http://gatling.io/docs/2.1.6/http/http_check.html?highlight=findall), findall saves the attribute as a **Seq[T]**, which could be your problem. But please elaborate.

---

<div class="post-metadata">

**Author:** ![lukas\_lang](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lukas\_lang](https://community.gatling.io/u/lukas_lang)\
**Post date:** [June 25, 2015, 5:03pm UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/4 "2015-06-25T17:03:08Z")

</div>

Thanks for response.  
My “list1” returns List (2, 5), but when i try to call function like “.exec(spustanie($list1, count1))” it tells me that variable is not defined. ☹

Dňa štvrtok, 25. júna 2015 18:56:45 UTC+2 Andrew Krischer napísal(-a):

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [June 25, 2015, 5:18pm UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/5 "2015-06-25T17:18:37Z")

</div>

If you call println(session) just before calling spustanien what do you see?

---

<div class="post-metadata">

**Author:** ![lukas\_lang](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lukas\_lang](https://community.gatling.io/u/lukas_lang)\
**Post date:** [June 26, 2015, 5:10am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/6 "2015-06-26T05:10:51Z")

</div>

I get this:

`Session(teotest89,7029719221769583319-0,Map(count1 -> 2, count2 -> 2, list1 -> List(2, 5), list2 -> List(0, 3), Ziak_id -> F000000060, <cookies and stuff> ... ),1435150367172,22,OK,List(),<function1>)`

And when i try something like this:

`  
.exec(spustanie(list1,count1))

`

it gives me “not found: value list1” and “not found: value count1”

Dňa štvrtok, 25. júna 2015 19:18:40 UTC+2 Stéphane Landelle napísal(-a):

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [June 26, 2015, 5:47am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/7 "2015-06-26T05:47:49Z")

</div>

Are you sure you’re saving and pulling from the SAME virtual user?  
Could you post a full gist?

---

<div class="post-metadata">

**Author:** ![lukas\_lang](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lukas\_lang](https://community.gatling.io/u/lukas_lang)\
**Post date:** [June 26, 2015, 6:02am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/8 "2015-06-26T06:02:09Z")

</div>

sure, here it is:  
[https://gist.github.com/anonymous/5426cbf7446c3fe10bb7](https://gist.github.com/anonymous/5426cbf7446c3fe10bb7)

Dňa piatok, 26. júna 2015 7:47:51 UTC+2 Stéphane Landelle napísal(-a):

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [June 26, 2015, 6:07am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/9 "2015-06-26T06:07:07Z")

</div>

Sorry, but this doesn’t compile:

```auto

		.exec(spustanie(list1,list2,count1,count2))

```

---

<div class="post-metadata">

**Author:** ![lukas\_lang](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lukas\_lang](https://community.gatling.io/u/lukas_lang)\
**Post date:** [June 26, 2015, 6:09am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/10 "2015-06-26T06:09:27Z")

</div>

That is the thing. I dont know how to call “spustanie” with value “list1” as parameter, because it says "not found: value list1 "

Dňa piatok, 26. júna 2015 8:07:08 UTC+2 Stéphane Landelle napísal(-a):

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [June 26, 2015, 6:20am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/11 "2015-06-26T06:20:38Z")

</div>

The original way is supposed to work:

.exec(session =\> {  
spustanie(session(“list1”).as[List[String]],session(“count1”).as[Int])  
session  
})

---

<div class="post-metadata">

**Author:** ![lukas\_lang](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lukas\_lang](https://community.gatling.io/u/lukas_lang)\
**Post date:** [June 26, 2015, 6:37am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/12 "2015-06-26T06:37:08Z")

</div>

That way its compiled OK, it just doesnt really run that function.

`

> begin (OK=1 KO=0 )  
> zacat testovanie2 (OK=1 KO=0 )  
> notify\_zacat (OK=1 KO=0 )  
> logout (OK=1 KO=0 )

`

Dňa piatok, 26. júna 2015 8:20:39 UTC+2 Stéphane Landelle napísal(-a):

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [June 26, 2015, 6:57am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/13 "2015-06-26T06:57:49Z")

</div>

Now I get it. A perfect example of “it doesn’t work” is not the proper way of reporting an issue.

You can’t try to trigger requests from an exec(function).  
If you want to have conditional blocks, you have to use the proper DSL components, such as doIf.

---

<div class="post-metadata">

**Author:** ![lukas\_lang](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lukas\_lang](https://community.gatling.io/u/lukas_lang)\
**Post date:** [June 29, 2015, 6:40am UTC](https://community.gatling.io/t/using-variable-in-function-call/2382/14 "2015-06-29T06:40:35Z")

</div>

Ah i see.  
I changed logic a little, now i can use only “repeat”.  
Thank for all help 🙂

Lukas

Dňa piatok, 26. júna 2015 8:57:52 UTC+2 Stéphane Landelle napísal(-a):
