# How to concatenate contents of two string arrays with same number of elements

**URL:** https://discuss.elastic.co/t/how-to-concatenate-contents-of-two-string-arrays-with-same-number-of-elements/256
**Category:** Logstash
**Created:** [May 6, 2015, 5:31am UTC](https://discuss.elastic.co/t/how-to-concatenate-contents-of-two-string-arrays-with-same-number-of-elements/256 "2015-05-06T05:31:28Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![markus](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@markus](https://discuss.elastic.co/u/markus)
#### Post date: [May 6, 2015, 5:31am UTC](https://discuss.elastic.co/t/how-to-concatenate-contents-of-two-string-arrays-with-same-number-of-elements/256/1 "2015-05-06T05:31:28Z")

</div>

Hi,  
I have to string arrays with the same number of elements. Is it possible to create a new string array (or overwrite one of the existing ones) containing the string concatentation of the elements from both arrays at the same index.

Say:

```
Array A:
  ->foo
  ->forest

Array B:
  ->bar
  ->gump

Result:
   ->foobar
   ->forestgump

```

Any help appreciated!  
Bye,  
Markus

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [May 6, 2015, 5:53am UTC](https://discuss.elastic.co/t/how-to-concatenate-contents-of-two-string-arrays-with-same-number-of-elements/256/2 "2015-05-06T05:53:08Z")

</div>

I don't believe there's any stock filter that does this, but a Ruby filter can do the job:

```
ruby {
  code => "
    event['zipped'] = event['array1'].zip(event['array2']).map! { |item|
      item[0] + item[1]
    }
  "
}

```

zip() joins the two arrays to an array where each element is a two-element array of the corresponding items, in your example [["foo", "bar"], ["forest", "gump"]], and the map operation flattens the array via concatenation.

(Someone with better Ruby-fu can probably suggest a more elegant solution.)

---

<div class="post-metadata">

### Author: ![markus](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@markus](https://discuss.elastic.co/u/markus)
#### Post date: [May 6, 2015, 6:44am UTC](https://discuss.elastic.co/t/how-to-concatenate-contents-of-two-string-arrays-with-same-number-of-elements/256/3 "2015-05-06T06:44:38Z")

</div>

Thanks. I suspected that and was just starting a ruby tutorial when your post came in (one new language per year:-))  
Just if any one stumbles accross Magnus' solution. You have to take care that the input arrays are not null (or nil that is in ruby). Otherwise you get NoSuchMethod errors...

So do something like:

```
if(!event['array1'].nil?)
   event['zipped'] = event['array1'].zip(event['array2']).map! { |item|
       item[0] +'; '+ item[1]
   }
end
```

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [May 6, 2015, 5:34pm UTC](https://discuss.elastic.co/t/how-to-concatenate-contents-of-two-string-arrays-with-same-number-of-elements/256/4 "2015-05-06T17:34:00Z")

</div>

Indeed—and one should also check that the arrays have the same number of elements and that the fields exist in the first place.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 5:40am UTC](https://discuss.elastic.co/t/how-to-concatenate-contents-of-two-string-arrays-with-same-number-of-elements/256/5 "2017-07-06T05:40:13Z")

</div>


