# Concatenate multi arrays into one using ruby

**URL:** https://discuss.elastic.co/t/concatenate-multi-arrays-into-one-using-ruby/186979
**Category:** Logstash
**Created:** [June 23, 2019, 4:17am UTC](https://discuss.elastic.co/t/concatenate-multi-arrays-into-one-using-ruby/186979 "2019-06-23T04:17:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sahere37](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sahere37](https://discuss.elastic.co/u/sahere37)
#### Post date: [June 23, 2019, 4:17am UTC](https://discuss.elastic.co/t/concatenate-multi-arrays-into-one-using-ruby/186979/1 "2019-06-23T04:17:22Z")

</div>

hi all,  
I have following arrays as following:  
f1={a1,a2,a3,a4}  
f2={b1,b2,b3,b4}  
f3={c1,c2,c3,c4}  
f4={d1,d2,d3,d4}

I want to merge these arrays into one array as following which data of each element of new array separated by ":"  
result={[a1:b1:c1:d1],[a2:b2:c2:d2],[a3:b3:c3:d3],[a4:b4:c4:d4]}

after that i want to use split filter on "result" field to separate the arrays elements.  
how can i do above merging in ruby filter?

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [June 23, 2019, 9:52am UTC](https://discuss.elastic.co/t/concatenate-multi-arrays-into-one-using-ruby/186979/2 "2019-06-23T09:52:03Z")

</div>

If you want to set a field as an array of existing fields then the general form would be

```
ruby { code => 'event.set("result", [event.get("foo"), event.get("bar")])' }
```

---

<div class="post-metadata">

### Author: ![sahere37](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sahere37](https://discuss.elastic.co/u/sahere37)
#### Post date: [June 23, 2019, 10:09am UTC](https://discuss.elastic.co/t/concatenate-multi-arrays-into-one-using-ruby/186979/3 "2019-06-23T10:09:16Z")

</div>

it doesn't seem it leads to following format:

a1:b1:c1:d1

actually i want to concatenate the elements of multiple array into one array so that component of ith element of new array consist of componenets of ith elemement of other arrays so that separated using ":"

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [June 23, 2019, 1:51pm UTC](https://discuss.elastic.co/t/concatenate-multi-arrays-into-one-using-ruby/186979/4 "2019-06-23T13:51:07Z")

</div>

It is unclear what you want, since you use {} for things that you say are arrays and square brackets for things that cannot be arrays. However, you could try something like this

```
    ruby {
        code => '
            f1 = event.get("f1")
            f2 = event.get("f2")
            f3 = event.get("f3")
            f4 = event.get("f4")
            a = []
            f1.each_index { |i|
                a << "#{f1[i]}:#{f2[i]}:#{f3[i]}:#{f4[i]}"
            }
            event.set("result", a)
        '
    }

```

which results in

```
    "result" => [
    [0] "a1:b1:c1:d1",
    [1] "a2:b2:c2:d2",
    [2] "a3:b3:c3:d3",
    [3] "a4:b4:c4:d4"
],
```

---

<div class="post-metadata">

### Author: ![sahere37](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sahere37](https://discuss.elastic.co/u/sahere37)
#### Post date: [June 24, 2019, 5:28am UTC](https://discuss.elastic.co/t/concatenate-multi-arrays-into-one-using-ruby/186979/5 "2019-06-24T05:28:47Z")

</div>

many thanks. @Badger  
I used it but following result had been returned:  
"result": [  
"#{f1[i]}:#{f2[i]}:#{f3[i]}:#{f4[i]}",  
"#{f1[i]}:#{f2[i]}:#{f3[i]}:#{f4[i]}",  
"#{f1[i]}:#{f2[i]}:#{f3[i]}:#{f4[i]}",  
"#{f1[i]}:#{f2[i]}:#{f3[i]}:#{f4[i]}"  
],

---

<div class="post-metadata">

### Author: ![sahere37](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sahere37](https://discuss.elastic.co/u/sahere37)
#### Post date: [June 24, 2019, 7:07am UTC](https://discuss.elastic.co/t/concatenate-multi-arrays-into-one-using-ruby/186979/6 "2019-06-24T07:07:33Z")

</div>

I solved the issue using following code; difference is "," is instead of ":"

ruby {  
code =\> "  
event.set('result',event.get('f1').zip(event.get('f2'),event.get('f3'),event.get('f4')))  
"  
}

now there is another issue; i want to split the result to multi events based on the array's element; therefore, i want to have for event as following:  
event 1: {a1:b1:c1:d1}  
event 2:{a2:b2:c2:d2}  
event 3: {a3:b3:c3:d3}  
event 4: {a4:b4:c4:d4}  
fore this purpose, i used split filter as following:  
split {  
field =\> "result"  
}  
but following warning has been found at the log:

> [WARN][logstash.outputs.elasticsearch] Could not index event to Elasticsearch. {:status=\>400, :action=\>["index", {:\_id=\>nil, :\_index=\>"a\_totaltx14\_1398-03-31", :\_type=\>"doc", :routing=\>nil}, #LogStash::Event:0x7b3dbee2], :response=\>{"index"=\>{"\_index"=\>"a\_totaltx14\_1398-03-31", "\_type"=\>"doc", "\_id"=\>"9UtHiGsBPU7RNT0U0c26", "status"=\>400, "error"=\>{"type"=\>"illegal\_argument\_exception", "reason"=\>"mapper [result] of different type, current\_type [long], merged\_type [text]"}}}}

could you please advise me about this?

---

<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 22, 2019, 7:07am UTC](https://discuss.elastic.co/t/concatenate-multi-arrays-into-one-using-ruby/186979/7 "2019-07-22T07:07:37Z")

</div>

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