# Ruby code to generate fields dynamically based on csv

**URL:** <https://discuss.elastic.co/t/ruby-code-to-generate-fields-dynamically-based-on-csv/307276>\
**Category:** Logstash\
**Created:** [June 15, 2022, 12:43pm UTC](https://discuss.elastic.co/t/ruby-code-to-generate-fields-dynamically-based-on-csv/307276 "2022-06-15T12:43:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SreelekhaKovuri](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@SreelekhaKovuri](https://discuss.elastic.co/u/SreelekhaKovuri)\
**Post date:** [June 15, 2022, 12:43pm UTC](https://discuss.elastic.co/t/ruby-code-to-generate-fields-dynamically-based-on-csv/307276/1 "2022-06-15T12:43:39Z")

</div>

I need dynamically generated fields based on the CSV in the logs.  
In this case my csv format can be in any of the following ways:  
CSV =  
CSV = 0  
CSV = 1111;2222;3333;4444444;5555  
CSV = 1;2;3;4;5;6;7;8;9;10

So, I want the fields to be created in the following way:  
CSV01 = 1  
CSV02 = 2  
CSV03 = 3  
.  
.  
.

For this I tired the following Ruby code in filter plugin

```auto
 ruby {
                        code => '
                                if event.get("CSV") != ""
                                        CSV_Numbers = event.get("CSV").split(";")
                                        for csvNumber in CSV_Numbers
                                                IndexValue = event.get("CSV").index("csvNumber")
                                                fieldName = event.set("CSV0" + IndexValue)
                                                event.set("fieldName", csvNumber)
                                        end
                                end
                              '
                }

```

But I'm not able to generate fields dynamically. Can someone please help me in this case.

---

<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 15, 2022, 5:04pm UTC](https://discuss.elastic.co/t/ruby-code-to-generate-fields-dynamically-based-on-csv/307276/2 "2022-06-15T17:04:14Z")

</div>

There are several issues with your code. In Ruby names that start with an upper case letter are constants, not variables, so once the first event has set CSV\_Numbers it cannot be changed. Similarly for IndexValue. Also, you want fieldName to be a ruby variable, not a field on the event, so do not use event.set.

I would do this using

```
    ruby {
        code => '
            csv = event.get("CSV")
            if csv != ""
                csvNumbers = csv.split(";")
                csvNumbers.each_index { |x|
                    event.set("CSV0#{x}", csvNumbers[x])
                }
            end

        '
    }
```

---

<div class="post-metadata">

**Author:** ![SreelekhaKovuri](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@SreelekhaKovuri](https://discuss.elastic.co/u/SreelekhaKovuri)\
**Post date:** [June 17, 2022, 11:12am UTC](https://discuss.elastic.co/t/ruby-code-to-generate-fields-dynamically-based-on-csv/307276/3 "2022-06-17T11:12:19Z")

</div>

Thanks @Badger !!  
That actually helped.

---

<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 15, 2022, 11:12am UTC](https://discuss.elastic.co/t/ruby-code-to-generate-fields-dynamically-based-on-csv/307276/4 "2022-07-15T11:12:24Z")

</div>

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