# Creating an array of values from a combination of range and list values

**URL:** <https://discuss.elastic.co/t/creating-an-array-of-values-from-a-combination-of-range-and-list-values/191274>\
**Category:** Logstash\
**Created:** [July 18, 2019, 8:42pm UTC](https://discuss.elastic.co/t/creating-an-array-of-values-from-a-combination-of-range-and-list-values/191274 "2019-07-18T20:42:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cromagnon35](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cromagnon35](https://discuss.elastic.co/u/cromagnon35)\
**Post date:** [July 18, 2019, 8:42pm UTC](https://discuss.elastic.co/t/creating-an-array-of-values-from-a-combination-of-range-and-list-values/191274/1 "2019-07-18T20:42:38Z")

</div>

Hello, I've got an interesting problem I'm hoping someone might be able to assist with.

I'm using logstash to ingest data from Oracle which in by itself is working fine. However, I have a field called ziparea which has any combination of the left 3 digits of a given zipcode (we call them geozips), I need to parse this into an array called zip\_area in which each distinct value is contained.

For example: "316-320,398"  
should create an array : ["316", "317", "318", "319", "398"]

I've done this successfully using ruby via:  
zip\_area =   
split\_zip = zip.split(',')  
split\_zip.each{|zip|  
if zip.length == 3  
zip\_area \<\< zip  
else  
split\_zip = zip.split('-')  
beg\_zip = split\_zip[0].to\_i  
end\_zip = split\_zip[1].to\_i  
for z in beg\_zip...end\_zip + 1 do  
zip\_string = z.to\_s  
for i in zip\_string.length...3 do  
zip\_string = "0" + zip\_string  
end  
zip\_area \<\< zip\_string  
end  
end  
}  
puts "Array content: #{zip\_area}"

but I'm unsure how to integrate this with the logstash conf file. I could go either with an external file or it's not so big that inline wouldn't work.

I'm far from a guru in either logstash or ruby, so any assist would be welcome.

---

<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:** [July 18, 2019, 10:10pm UTC](https://discuss.elastic.co/t/creating-an-array-of-values-from-a-combination-of-range-and-list-values/191274/2 "2019-07-18T22:10:29Z")

</div>

```
    ruby {
        code => '
            zip_area = []
            split_zip = event.get("zip").split(",")
            split_zip.each { |zip|
                if zip.length == 3
                    zip_area << zip
                else
                    split_zip = zip.split("-")
                    beg_zip = split_zip[0].to_i
                    end_zip = split_zip[1].to_i
                    for z in beg_zip...end_zip + 1 do
                        zip_string = z.to_s
                        for i in zip_string.length...3 do
                            zip_string = "0" + zip_string
                        end
                        zip_area << zip_string
                    end
                end
            }
            event.set("zipAreas", zip_area)
        '
    }

```

will convert

```
       "zip" => "316-320,398",

```

into

```
  "zipAreas" => [
    [0] "316",
    [1] "317",
    [2] "318",
    [3] "319",
    [4] "320",
    [5] "398"
],
```

---

<div class="post-metadata">

**Author:** ![cromagnon35](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cromagnon35](https://discuss.elastic.co/u/cromagnon35)\
**Post date:** [July 19, 2019, 2:46pm UTC](https://discuss.elastic.co/t/creating-an-array-of-values-from-a-combination-of-range-and-list-values/191274/3 "2019-07-19T14:46:56Z")

</div>

Thanks Badger! I'll give it a try and let you know.

---

<div class="post-metadata">

**Author:** ![cromagnon35](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cromagnon35](https://discuss.elastic.co/u/cromagnon35)\
**Post date:** [July 19, 2019, 4:07pm UTC](https://discuss.elastic.co/t/creating-an-array-of-values-from-a-combination-of-range-and-list-values/191274/4 "2019-07-19T16:07:39Z")

</div>

Today's UDAMAN award goes to Badger! A couple of minor tweaks to work inside a bash string (I'm using a bash script to create the conf file programatically) and rename of the the zip field to the incomming ziparea field and it's golden. I owe you a beer now :-).

Here's the section in the resulting conf file for reference:  
ruby {  
code =\> "zip\_area =   
split\_zip = event.get('ziparea').split(',')  
split\_zip.each { |ziparea|  
if ziparea.length == 3  
zip\_area \<\< ziparea  
else  
split\_zip = ziparea.split('-')  
beg\_zip = split\_zip[0].to\_i  
end\_zip = split\_zip[1].to\_i  
for z in beg\_zip...end\_zip + 1 do  
zip\_string = z.to\_s  
for i in zip\_string.length...3 do  
zip\_string = '0' + zip\_string  
end  
zip\_area \<\< zip\_string  
end  
end  
}  
event.set('zipAreas', zip\_area)  
"  
}

---

<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:** [August 16, 2019, 4:07pm UTC](https://discuss.elastic.co/t/creating-an-array-of-values-from-a-combination-of-range-and-list-values/191274/5 "2019-08-16T16:07:41Z")

</div>

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