# Get value from a seq in session?

**URL:** <https://community.gatling.io/t/get-value-from-a-seq-in-session/793>\
**Category:** Gatling (Open-Source)\
**Created:** [January 28, 2014, 1:26am UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793 "2014-01-28T01:26:27Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kan\_Wu](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@Kan\_Wu](https://community.gatling.io/u/Kan_Wu)\
**Post date:** [January 28, 2014, 1:26am UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/1 "2014-01-28T01:26:27Z")

</div>

Hey Stéphane,

sorry to bother you again. i may need some tips to make this working.

i have two seq lists from previous regex check, list A, list B. then i use foreach to iterate list A, and use counter to identify the same index element in list B.  
here is the code:

foreach("${ListA}",“A”,“counter”) {  
exec(  
http(“Get”)  
.get("/")  
.queryParam(""“A”"", “”"${A}""")  
.queryParam(""“B”"", session =\> session(“ListB”)[session(“counter”).as[Int]])  
.pause(0 milliseconds, Properties.requestSleep milliseconds)

but the compile didnt pass, 3 errors are all about the format of the brackets.

i checked in sessions that both lists are complete and correct.

Thanks!

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 28, 2014, 8:50am UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/2 "2014-01-28T08:50:48Z")

</div>

With as:

.queryParam(""“B”"", session =\> {  
val listB = session(“ListB”).as[Seq[String]]  
val counter = session(“counter”).as[Int]  
listB(counter)  
})

With validate (safer):

.queryParam(""“B”"", session =\>  
for {  
listB ← session(“ListB”).validate[Seq[String]]  
counter ← session(“counter”).validate[Int]  
} yield listB(counter)  
)

---

<div class="post-metadata">

**Author:** ![Kan\_Wu](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@Kan\_Wu](https://community.gatling.io/u/Kan_Wu)\
**Post date:** [January 28, 2014, 5:51pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/3 "2014-01-28T17:51:07Z")

</div>

Works perfectly in gatling 2, thanks a lot!!

在 2014年1月28日星期二UTC-8上午12时50分48秒，Stéphane Landelle写道：

---

<div class="post-metadata">

**Author:** ![Kan\_Wu](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@Kan\_Wu](https://community.gatling.io/u/Kan_Wu)\
**Post date:** [January 28, 2014, 7:29pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/4 "2014-01-28T19:29:54Z")

</div>

is is possible to re-use listB in next exec? for example, i dont want use counter anymore, but a random value.

.queryParam(""“weight”"", session =\> {  
val listB = session(“ListB”).as[Seq[String]]  
val random = getRand(listB.size)  
listB(random)  
})

in next exec, i want to re-use the random value and listB if possible, how can i do this? compilation said value listB, random not found.

在 2014年1月28日星期二UTC-8上午12时50分48秒，Stéphane Landelle写道：

---

<div class="post-metadata">

**Author:** ![Kan\_Wu](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@Kan\_Wu](https://community.gatling.io/u/Kan_Wu)\
**Post date:** [January 28, 2014, 8:36pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/5 "2014-01-28T20:36:03Z")

</div>

i think i can use session set attribute to set the random.

在 2014年1月28日星期二UTC-8上午11时29分54秒，Kan Wu写道：

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 28, 2014, 9:55pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/6 "2014-01-28T21:55:09Z")

</div>

I feel stupid: I totally forgot that Gatling EL supports this out of the box!

“${ListB(counter)}”

“${ListB.random}”

---

<div class="post-metadata">

**Author:** ![Kan\_Wu](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@Kan\_Wu](https://community.gatling.io/u/Kan_Wu)\
**Post date:** [January 28, 2014, 11:54pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/7 "2014-01-28T23:54:23Z")

</div>

nice. but if i want function like indexOf, i have to save that the seq to a list first? That’s what i’m doing now and it works fine.

在 2014年1月28日星期二UTC-8下午1时55分09秒，Stéphane Landelle写道：

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 29, 2014, 8:12am UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/8 "2014-01-29T08:12:24Z")

</div>

Sorry, I don’t get it. indexOf is defined on Seq (which is a trait that List extends).

---

<div class="post-metadata">

**Author:** ![Kan\_Wu](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@Kan\_Wu](https://community.gatling.io/u/Kan_Wu)\
**Post date:** [January 29, 2014, 5:41pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/9 "2014-01-29T17:41:52Z")

</div>

sorry. i didnt say it clearly. this is my code of how doing it:

exec(session =\> {  
val tList = session(“typeList”).as[Seq[String]]  
val wList = session(“weightList”).as[Seq[String]]  
val index = tList.indexOf(“0”)  
val weight = wList(index)  
session.set(“index”,index)  
session.set(“weight”,weight)  
})

we can simply use ${typeList.indexOf(“0”)} and skip saving to a local val first?

在 2014年1月29日星期三UTC-8上午12时12分24秒，Stéphane Landelle写道：

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 29, 2014, 5:44pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/10 "2014-01-29T17:44:16Z")

</div>

indexOf isn’t supported in Gatling EL.

Gatling EL is meant to be very simple and not a full blown kind of dynamic language inside Scala code.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 29, 2014, 5:45pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/11 "2014-01-29T17:45:54Z")

</div>

Where does this “0” attribute come from?

---

<div class="post-metadata">

**Author:** ![Kan\_Wu](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@Kan\_Wu](https://community.gatling.io/u/Kan_Wu)\
**Post date:** [January 29, 2014, 5:46pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/12 "2014-01-29T17:46:52Z")

</div>

this is what i expected too. my code is working fine. Thanks a lot for your response!

btw, i sent you an email in detail of the issue we talked about.

在 2014年1月29日星期三UTC-8上午9时44分16秒，Stéphane Landelle写道：

---

<div class="post-metadata">

**Author:** ![Kan\_Wu](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@Kan\_Wu](https://community.gatling.io/u/Kan_Wu)\
**Post date:** [January 29, 2014, 5:49pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/13 "2014-01-29T17:49:23Z")

</div>

“0” is a value of the seq. i want to know the index of “0” in tList, then get the same index element from list wList.

在 2014年1月29日星期三UTC-8上午9时45分54秒，Stéphane Landelle写道：

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 29, 2014, 9:07pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/14 "2014-01-29T21:07:22Z")

</div>

If tList and wList are supposed to work together, I guess I’d first zip them together (so I’d get a Seq[(String, String)]), loop over the result and add a exec(function) that would expose the Tuple2 current element into 2 dedicated attributes.

Get it?

---

<div class="post-metadata">

**Author:** ![Kan\_Wu](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@Kan\_Wu](https://community.gatling.io/u/Kan_Wu)\
**Post date:** [January 29, 2014, 11:06pm UTC](https://community.gatling.io/t/get-value-from-a-seq-in-session/793/15 "2014-01-29T23:06:02Z")

</div>

yes. i see your point here but we use two lists in separate places but only need to get the index value. i will try your suggestion if it could life efficiency. Thanks for helping out!

在 2014年1月29日星期三UTC-8下午1时07分22秒，Stéphane Landelle写道：
