# Gatling-Scala : How to consolidated( group strings) from a loop output to a String Array or Vector Array

**URL:** <https://community.gatling.io/t/gatling-scala-how-to-consolidated-group-strings-from-a-loop-output-to-a-string-array-or-vector-array/7675>\
**Category:** Gatling (Open-Source)\
**Tags:** gatling-academy, release\
**Created:** [February 27, 2023, 3:12pm UTC](https://community.gatling.io/t/gatling-scala-how-to-consolidated-group-strings-from-a-loop-output-to-a-string-array-or-vector-array/7675 "2023-02-27T15:12:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Monika](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Monika](https://community.gatling.io/u/Monika)\
**Post date:** [February 27, 2023, 3:12pm UTC](https://community.gatling.io/t/gatling-scala-how-to-consolidated-group-strings-from-a-loop-output-to-a-string-array-or-vector-array/7675/1 "2023-02-27T15:12:02Z")

</div>

Gatling-Scala : How to consolidated( group strings) from a loop output to a String Array or Vector Array

1. I need to loop with array value providing string output
2. consolidated all the retrieved string output as an array
3. use the consolidated array in next bulk api request.

My logic is not working, kindly help me.

**Please help me if there are easier methods with gatling keywords**

```auto
 var i = 0 // initialising i to 0 to increase the array index value in loop
.
.
...check(jsonPath("$.id").saveAs("ArrayValues"))
// saving this array to use in loop and at defining arraylength at last of the loop

```

//loop

```auto
    .foreach("#{ArrayValues}", "VValues") {
      exec(http("perftest values")
        .post("perftest")
        .body(StringBody(session =>
          s"""{
             |"id": "",
             |"perfTypeId": "00334-0000-0000-0345",
             |"legs":[{
             | "valueTypeId": "111-220-3300-0444",
             | "values": "${session("VValues").as[String]}"
             | }]
             | }""".stripMargin)).asJson
        .check(jsonPath("$.id").saveAs("perfvalue_singlevalue"))
        .check(status.is(201)))
        .exec(session => {
          val valuearraylen: Vector[String] = session("ArrayValues").as[Vector[String]]
          val perfvalue_array: Array[String] = new Array[String](valuearraylen.length)
          perfvalue_array(i) = session("perfvalue_singlevalue").as[String] 
// ERROR in above logic
 // saving value in updated array index
          i = i + 1. // moving to next array index
          session.set("string_collected_array", perfvalue_array)
        })

```

//provide the created array value to next api request

```auto
.exec(http("Bulk_perftest")
    .delete("/perf/bulk")
    .body(StringBody(session => s"""[ 
    "${session("string_collected_array").as[Vector[String]].mkString("\",\"")}"]"""))
    .check(status.is(204)))

```

// **ERROR** cannot pass the value - error no attribute name defined “string\_collected\_array”

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [February 27, 2023, 3:50pm UTC](https://community.gatling.io/t/gatling-scala-how-to-consolidated-group-strings-from-a-loop-output-to-a-string-array-or-vector-array/7675/2 "2023-02-27T15:50:33Z")

</div>

Hi @Monika,

It seems that scala is not your cup of tea. Do you know that you can use Java or Kotlin for creating Gatling simulations?

I’m not sure what you expect to achieve. I will try to rephrase it with my own word:

Creating a scenario (describe 1 user journey), that will loop over an array and save the result to be able to bulk a final request.

My approach will be:

- create an empty list (or ensure it is empty) in session before the loop.
- in the loop, prepend to the list (`session.set("myList", myNewValue :: session("myList").as[List[String]])`)
- after the loop, creating the wanted String (`session("myList").as[List[String]].mkString(", ")`) for the execution.

Notes:

- Are you in the same scenario? (session is not shared between users)
- Avoid (when possible) global variable
- `Array` is not auto-extensible

Does it help?

Cheers!

---

<div class="post-metadata">

**Author:** ![Monika](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Monika](https://community.gatling.io/u/Monika)\
**Post date:** [February 27, 2023, 6:21pm UTC](https://community.gatling.io/t/gatling-scala-how-to-consolidated-group-strings-from-a-loop-output-to-a-string-array-or-vector-array/7675/3 "2023-02-27T18:21:37Z")

</div>

@sbrevet thank you for helping.  
All the existing scripts are done in scala, so emp wants only scala scripting.

can you please provide an example.  
I was trying with list but its completely gone mess.

```
.exec(session => {
  val valuesperf = List[String]()
  session.set("valuesperf", valuesperf)
})

.foreach("#{ArrayValues}", "VValues") {
  exec(http("perftest values")
    .post("perftest")
    .body(StringBody(session =>
      s"""{
         |"id": "",
         |"perfTypeId": "00334-0000-0000-0345",
         |"legs":[{
         | "valueTypeId": "111-220-3300-0444",
         | "values": "${session("VValues").as[String]}"
         | }]
         | }""".stripMargin)).asJson
    .check(jsonPath("$.id").saveAs("perfvalue_singlevalue"))
    .check(status.is(201)))
    .exec(session => {
      session.set("valuesperf", valuesperf :: session("valuesperf").as[List[String]])
      session("valuesperf").as[List[String]].mkString(", ")
    }
      session.set("string_collected_array", perfvalue_array)
    })

```

.exec(http(“Bulk\_perftest”)  
.delete(“/perf/bulk”)  
.body(StringBody(session =\> s"“”[  
“${session(“string\_collected\_array”).as[Vector[String]].mkString(”","“)}”]“”"))  
.check(status.is(204)))

- I am in same scenario

Please help me.

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [February 27, 2023, 9:39pm UTC](https://community.gatling.io/t/gatling-scala-how-to-consolidated-group-strings-from-a-loop-output-to-a-string-array-or-vector-array/7675/4 "2023-02-27T21:39:00Z")

</div>

```scala
.exec(_.set("valuesperf", List.empty[String])
.foreach("#{ArrayValues}", "VValues") {
  exec(http("perftest values")
    .post("perftest")
    .body(StringBody(session =>
      s"""{
         |"id": "",
         |"perfTypeId": "00334-0000-0000-0345",
         |"legs":[{
         | "valueTypeId": "111-220-3300-0444",
         | "values": "${session("VValues").as[String]}"
         | }]
         | }""".stripMargin)).asJson
    .check(jsonPath("$.id").saveAs("perfvalue_singlevalue"))
    .check(status.is(201)))
  .exec(_.set("valuesperf", session("perfvalue_singlevalue").as[String] :: session("valuesperf").as[List[String]])
}).exec(session => {
  println(session("valuesperf").as[List[String]].mkString(", ")) // only to show the resulting value
  session
})
.exec(http("Bulk_perftest")
  .delete("/perf/bulk")
  .body(StringBody(session => s"""["${session("valuesperf").as[List[String]].mkString("\", \"")}"]"""))
  .check(status.is(204)))

```

Notes:

- `session` is immutable, you have to return the updated value
- preprend the list with the current value
- use the accumulated list for the final request

Cheers!

---

<div class="post-metadata">

**Author:** ![Monika](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Monika](https://community.gatling.io/u/Monika)\
**Post date:** [February 28, 2023, 11:04am UTC](https://community.gatling.io/t/gatling-scala-how-to-consolidated-group-strings-from-a-loop-output-to-a-string-array-or-vector-array/7675/5 "2023-02-28T11:04:47Z")

</div>

@sbrevet Thank you  
your logic solved my issues.

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [March 30, 2023, 11:05am UTC](https://community.gatling.io/t/gatling-scala-how-to-consolidated-group-strings-from-a-loop-output-to-a-string-array-or-vector-array/7675/6 "2023-03-30T11:05:36Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
