# Access dynamic array

**URL:** <https://discuss.elastic.co/t/access-dynamic-array/199728>\
**Category:** Logstash\
**Created:** [September 16, 2019, 9:10pm UTC](https://discuss.elastic.co/t/access-dynamic-array/199728 "2019-09-16T21:10:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guy\_E\_Bouchard](https://avatars.discourse-cdn.com/v4/letter/g/d2c977/32.png) [@Guy\_E\_Bouchard](https://discuss.elastic.co/u/Guy_E_Bouchard)\
**Post date:** [September 16, 2019, 9:10pm UTC](https://discuss.elastic.co/t/access-dynamic-array/199728/1 "2019-09-16T21:10:59Z")

</div>

Hi,

I can't figure that out, I'm pretty sure I missed something

I want to access the last line of this array.

```
    "logs" => [
        [0] 23:33:39 Sep 15, 2019 - Delivered "19-09-15_23-33-37.127558/file_01_01_01.xml",
        [1] 23:33:39 Sep 15, 2019 - Delivered "19-09-15_23-33-37.127558/file_02_02_02.xml", 
        [2] 23:33:39 Sep 15, 2019 - Delivered "19-09-15_23-33-37.127558/file_03_03_03.xml"
    ],

```

Since the number of row is dynamic the row number must change

I use those script to determine the last row number

```
ruby {	
  code => "
  new_value = event.get('logs').length
  event.set('number_of_row',new_value)
  "
  }

  ruby {
    code => "
    event.set('last_row_number', event.get('number_of_row') - 1);
    "
    } 

```

last\_row\_number == 2

`mutate {add_field => { "test" => "[logs][2]"}}`

Return good result

But I can't find the correct syntax to set this dynamically

`mutate {add_field => { "test" => "[logs][%{last_row_number}]"}}`

doesn't work

Thanks for your help`

---

<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:** [September 16, 2019, 9:57pm UTC](https://discuss.elastic.co/t/access-dynamic-array/199728/2 "2019-09-16T21:57:12Z")

</div>

Why not just do it in a single ruby filter?...

```
ruby { code => 'event.set("lastLine", event.get("logs")[-1])' }
```

---

<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:** [October 14, 2019, 9:57pm UTC](https://discuss.elastic.co/t/access-dynamic-array/199728/3 "2019-10-14T21:57:24Z")

</div>

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