# How to use ruby code to realize “i = i + 1" operation

**URL:** <https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646>\
**Category:** Logstash\
**Created:** [September 2, 2019, 6:08am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646 "2019-09-02T06:08:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![terrymu](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@terrymu](https://discuss.elastic.co/u/terrymu)\
**Post date:** [September 2, 2019, 6:08am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646/1 "2019-09-02T06:08:09Z")

</div>

Hi Experts,

I' m using ruby code to parse my target log.  
Here is the source code:

```
if "_xmlparsefailure" not in [tags]{
        ruby {
                code => '
                        res = []
                        pmMeasInfo = event.get("[parsed][measData][0][measInfo]")
                        pmMeasInfo.each do |pr|

                                hash = Hash.new
                                i = 0

                                pmmeasValue = pr["measValue"][0]
                                pmmeasType = pr["measType"][0]
                                hash["measObJ"] = pmmeasValue["measObjLdn"]

                                pmmeasType.each do |pt|
                                        key = pt["content"]
                                        value = pmmeasValue["r"][#i]["content"].to_i
                                        hash[key] = value
                                        i = #i + 1
                                end

                                res.push(hash)
                        end

                        event.set("pm",res)
                '
        }
}

```

but when I start the logstash, here is the error report is fired:

```
[2019-08-30T16:11:23,024][ERROR][org.logstash.Logstash] java.lang.IllegalStateException: Logstash stopped processing because of an error: (SyntaxError) (ruby filter code):18: syntax error, unexpected tIDENTIFIER
                                                    i = #i + 1

```

How can I realize “i = i + 1" operation in ruby code section?  
Thank you.,

---

<div class="post-metadata">

**Author:** ![terrymu](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@terrymu](https://discuss.elastic.co/u/terrymu)\
**Post date:** [September 2, 2019, 8:09am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646/2 "2019-09-02T08:09:12Z")

</div>

If I use global variable $i to replace local variable i, it still fired an issue in the below:

```
[2019-09-02T16:05:32,636][ERROR][org.logstash.Logstash] java.lang.IllegalStateException: Logstash stopped processing because of an error: (SyntaxError) (ruby filter code):18: syntax error, unexpected tGVAR
                                                $i = #$i + 1

```

Who can help me check it, this issue is blocking a long duration for me...

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [September 2, 2019, 8:39am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646/3 "2019-09-02T08:39:56Z")

</div>

As it is Ruby, there are a few ways, however I don't think `#i` works as you expect. the `#` character is a single line comment until EOL character.

What the compiler is seeing is

```auto
  value = pmmeasValue["r"][
  hash[key] = value
  i =
end

```

Just use `i` e.g. `value = pmmeasValue["r"][i]["content"].to_i`

To increment a variable you can use any of:

```auto
i = i + 1
i += 1
i = i.succ

```

---

<div class="post-metadata">

**Author:** ![terrymu](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@terrymu](https://discuss.elastic.co/u/terrymu)\
**Post date:** [September 3, 2019, 1:44am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646/5 "2019-09-03T01:44:34Z")

</div>

Hi expert,

You are right, i have removed the "#" in my code and showed as below:

```auto
if "_xmlparsefailure" not in [tags]{
        ruby {
                code => '
                        res = []
                        pmMeasInfo = event.get("[parsed][measData][0][measInfo]")
                        pmMeasInfo.each do |pr|

                                hash = Hash.new
                                i = 0

                                pmmeasValue = pr["measValue"][0]
                                pmmeasType = pr["measType"][0]
                                hash["measObJ"] = pmmeasValue["measObjLdn"]

                                pmmeasType.each do |pt|
                                        key = pt["content"]
                                        value = pmmeasValue["r"][i]["content"].to_i
                                        hash[key] = value
                                        i = i + 1
                                end

                                res.push(hash)
                        end

                        event.set("pm",res)
                '
        }
}

```

Now my pattern can be executed, but here is another issued logs when I parsed the target logs, would you please help me check it? Thank you.

> `2019-09-03T09:36:42,960][ERROR][logstash.filters.ruby] Ruby exception occurred: no implicit conversion of String into Integer`

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [September 3, 2019, 12:42pm UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646/6 "2019-09-03T12:42:33Z")

</div>

It is hard to help, I need more info.

Please you post an example of the value that the field `[parsed][measData]`.

---

<div class="post-metadata">

**Author:** ![terrymu](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@terrymu](https://discuss.elastic.co/u/terrymu)\
**Post date:** [September 4, 2019, 1:02am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646/8 "2019-09-04T01:02:59Z")

</div>

Hi Guyboertje,

I have adjust my ruby code, then this issue is resolved, the cause is i have referred the wrong data format that can't adapt the source log. Thank you.

But when I load my logstash script to parse my source log, i have met another performance issue.

`

> [2019-09-03T14:21:49,106][WARN][logstash.outputs.elasticsearch] Could not index event to Elasticsearch. {:status=\>400, :action=\>["index", {:\_id=\>nil, :\_index=\>"logstash-2019.09.03", :\_type=\>"\_doc", :\_routing=\>nil}, #LogStash::Event:0x1b6c342a], :response=\>{"index"=\>{"\_index"=\>"logstash-2019.09.03", "\_type"=\>"\_doc", "\_id"=\>"4\_PJ9WwB6iwMcN-ZT20T", "status"=\>400, "error"=\>{"type"=\>"illegal\_argument\_exception", "reason"=\>"Limit of total fields [1000] in index [logstash-2019.09.03] has been exceeded"}}}}

`  
Would you please help me check it? It seems a parameter that limit the max index count. Of course I have found a solution, but I don't know how to use it.

The detailed information you can access this ticket:

![](https://us1.discourse-cdn.com/elastic/original/3X/6/f/6f174333fe1fc7ab7f2f87ea6397016d21213dfe.png) [Regarding to logstash performance limit](https://discuss.elastic.co/t/regarding-to-logstash-performance-limit/197763) [Logstash](https://discuss.elastic.co/c/logstash)

> Hi experts, I have met a logstash performance limit issue, when I parse my log, logstash report this error log: ` [2019-09-03T14:21:49,106][WARN][logstash.outputs.elasticsearch] Could not index event to Elasticsearch. {:status=\>400, :action=\>["index", {:\_id=\>nil, :\_index=\>"logstash-2019.09.03", :\_type=\>"\_doc", :\_routing=\>nil}, #LogStash::Event:0x1b6c342a], :response=\>{"index"=\>{"\_index"=\>"logstash-2019.09.03", "\_type"=\>"\_doc", "\_id"=\>"4\_PJ9WwB6iwMcN-ZT20T", "status"=\>400, "error"=\>{"type"=\>"…

Thank you again.  
[/quote]

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [September 4, 2019, 9:19am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646/9 "2019-09-04T09:19:30Z")

</div>

Please put the latest error in a new post. More people will see it and help or it will help them if they search for similar.

---

<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 2, 2019, 9:19am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-realize-i-i-1-operation/197646/10 "2019-10-02T09:19:32Z")

</div>

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