# Use index and get value from an array in ruby logstash

**URL:** <https://discuss.elastic.co/t/use-index-and-get-value-from-an-array-in-ruby-logstash/85233>\
**Category:** Logstash\
**Created:** [May 10, 2017, 11:32am UTC](https://discuss.elastic.co/t/use-index-and-get-value-from-an-array-in-ruby-logstash/85233 "2017-05-10T11:32:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aditya\_Srivastava](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aditya_srivastava/32/43287_2.png) [@Aditya\_Srivastava](https://discuss.elastic.co/u/Aditya_Srivastava)\
**Post date:** [May 10, 2017, 11:32am UTC](https://discuss.elastic.co/t/use-index-and-get-value-from-an-array-in-ruby-logstash/85233/1 "2017-05-10T11:32:08Z")

</div>

I have 2 input fields

"id" =\> [  
[0] "0",  
[1] "3",  
[2] "5",  
[3] "7",  
[4] "9"  
],  
"data" =\> [  
[0] "01",  
[1] "03",  
[2] "05",  
[3] "07",  
[4] "09"  
],

My requirement is that I want to get the index of "id" field where value is "7".  
And then use the index value(of "7") to get the value in "data" for the same index(which should be 07)

To extract the index from id, below is the ruby code  
ruby {  
code =\> "  
event['IntegerValue'] = event['id'].index('7')  
"  
}  
It gives me the value 3 which is the index value in integer.

Now to get the value of data at same index, below is the ruby code  
event['RCODE'] = event['[data][IntegerValue]']

It always returns me the value at 0 index. Whatever I may try.

I also tried mutate filter

mutate{  
add\_field =\> { "RCODE" =\> "%{[data][IntegerValue]}" }  
}  
It again returns me value at 0 index which is 01 which is wrong. My desired output is 07 (in this case).  
If I pass static index value I get the correct response  
mutate{  
add\_field =\> { "RCODE" =\> "%{[data][3]}" }  
}  
Output is 07 which is the desired one.  
Please help what am I doing wrong here.

---

<div class="post-metadata">

**Author:** ![Aditya\_Srivastava](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aditya_srivastava/32/43287_2.png) [@Aditya\_Srivastava](https://discuss.elastic.co/u/Aditya_Srivastava)\
**Post date:** [May 10, 2017, 11:57am UTC](https://discuss.elastic.co/t/use-index-and-get-value-from-an-array-in-ruby-logstash/85233/2 "2017-05-10T11:57:45Z")

</div>

Ok. Got the solution.

event['RCODE'] = event['data'].at(event['IntegerValue'])

---

<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:** [June 7, 2017, 12:09pm UTC](https://discuss.elastic.co/t/use-index-and-get-value-from-an-array-in-ruby-logstash/85233/3 "2017-06-07T12:09:19Z")

</div>

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