# If condition loop on array

**URL:** https://discuss.elastic.co/t/if-condition-loop-on-array/336518
**Category:** Logstash
**Created:** [June 20, 2023, 11:59pm UTC](https://discuss.elastic.co/t/if-condition-loop-on-array/336518 "2023-06-20T23:59:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![plus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/plus/32/97297_2.png) [@plus](https://discuss.elastic.co/u/plus)
#### Post date: [June 20, 2023, 11:59pm UTC](https://discuss.elastic.co/t/if-condition-loop-on-array/336518/1 "2023-06-20T23:59:29Z")

</div>

{  
Hello, I was reading several posts how to loop through with array but I don't know how to iterate on each value and then rename.  
I tried with split but it creates a document for each value ( I want a doc with all values)  
I was able to use the following code but I would like to know how to iterate for each value (example)  
tabladiscos[0]["3.2.5.1.1.73"]  
tabladiscos[1]["3.2.5.1.1.73"]  
tabladiscos[2]["3.2.5.1.1.73"]...

I have this code:

```auto
input {
  snmp {
    tables => [{"name" => "tabladiscos" "columns" => [".1.3.6.1.4.1.232.3.2.5.1.1.73"]}]
    hosts => [
      {host => "udp:xx.xxx.xxx.xxx/161" community => "site1" version => "2c" retries => 2 timeout => 1000},
      {host => "udp:xx.xxx.xxx.xxx/161" community => "site1" version => "2c" retries => 2 timeout => 1000},
      {host => "udp:xx.xxx.xxx.xxx/161" community => "site1" version => "2c" retries => 2 timeout => 1000}
    ]
    mib_paths => ["/usr/share/snmp/mibs/"]
    interval => 120
    #OID_ROOT_SKIP = OID root digits to ignore in the event field name
    oid_root_skip => 7
  }
}
filter {
  if ([tabladiscos][0][3.2.5.1.1.73]){
    mutate {
      rename => {"[tabladiscos][**0**][3.2.5.1.1.73]" => "disk01"}
    }
  }if ([tabladiscos][1][3.2.5.1.1.73]){
    mutate {
      rename => {"[tabladiscos][**1**][3.2.5.1.1.73]" => "disk02"}
    }
  }if ([tabladiscos][2][3.2.5.1.1.73]){
    mutate {
      rename => {"[tabladiscos][**2**][3.2.5.1.1.73]" => "disk03"}
    }
    }else {
    drop {} }
}

```

in Kibana a JSON file is like this:

```auto
  "_index": "ilohost",
  "_type": "_doc",
  "_id": "gmUd25lBsPB1x",
  "_version": 1,
  "_score": 1,
  "_source": {
    "@timestamp": "2023-06-20T23:22:16.726Z",
    "@version": "1",
    "disk03": 2,
    "tabladiscos": [
      {
        "index": "0.16"
      },
      {
        "index": "0.17"
      },
      {
        "index": "0.18"
      },
      {
        "index": "0.19",
        "3.2.5.1.1.73": 2
      }
    ],
    "host": "xx.xxx.xxx.xx"
    "disk01": 2,
    "disk02": 2
  },
  "fields": {
    "tabladiscos.3.2.5.1.1.73": [
      2,
      2,
      2,
      2
    ],
    "disk03": [
      2
    ],
    "tabladiscos.index": [
      "0.16",
      "0.17",
      "0.18",
      "0.19"      
    ],
    "@version.keyword": [
      "1"
    ],
    "@timestamp": [
      "2023-06-20T23:22:16.726Z"
    ],
    "tabladiscos.index.keyword": [
      "0.16",
      "0.17",
      "0.18",
      "0.19"
      
    ],
    "@version": [
      "1"
    ],
    "host": [
      "xx.xxx.xxx.xx"
    ],
    "host.keyword": [
      "xx.xxx.xxx.xx"
    ],
    "disk01": [
      2
    ],
    "disk02": [
      2
    ]
  }
}

```

---

<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 21, 2023, 2:09am UTC](https://discuss.elastic.co/t/if-condition-loop-on-array/336518/2 "2023-06-21T02:09:52Z")

</div>

If there are a variable number of entries you would need a ruby filter. Something like

```
ruby {
    code => '
        discos = event.get("tabladiscos")
        if discos.is_A? Array
            target = 1
            discos.each_index { |i|
                if discos[i]["3.2.5.1.1.73"]
                    event.set("disk" + target.to_s.rjust(2, "0"), discos[i]["3.2.5.1.1.73"])
                    target += 1
                end
            }
        end
    '
}

```

I haven't tested that, but hopefully it helps.

---

<div class="post-metadata">

### Author: ![plus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/plus/32/97297_2.png) [@plus](https://discuss.elastic.co/u/plus)
#### Post date: [June 21, 2023, 7:23am UTC](https://discuss.elastic.co/t/if-condition-loop-on-array/336518/3 "2023-06-21T07:23:58Z")

</div>

Thanks Badger for this quick response, let me check if works and will answer here.  
I´m not familiar with ruby and I didn't know how to change each number (1,2, 3, ....) as a variable.

---

<div class="post-metadata">

### Author: ![plus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/plus/32/97297_2.png) [@plus](https://discuss.elastic.co/u/plus)
#### Post date: [June 21, 2023, 8:22am UTC](https://discuss.elastic.co/t/if-condition-loop-on-array/336518/4 "2023-06-21T08:22:29Z")

</div>

AMAZING, thank you very much !!!!!  
it works as I want 🤩  
could you tell me where can learn ruby? I´m a nobbie on it

I only had to modify the uppercase A and put as discos.is\_a?

This is JSON result and how it shows on kibana

```auto
{
  "_index": "ilohost",
  "_type": "_doc",
  "_id": "G8wE3YgBytP59SAoyF4M",
  "_version": 1,
  "_score": 1,
  "_source": {
    "NicEnableStatus": 2,
    "@version": "1",
    "InterfaceStatus": 2,
    "disk26": 2,
    "SelfTestErrors": 0,
    "disk15": 2,
    "@timestamp": "2023-06-21T08:14:18.019Z",
    "disk02": 2,
    "disk12": 2,
    "disk14": 2,
    "disk06": 2,
    "BatteryPercentCharged": 0,
    "disk25": 2,
    "disk17": 2,
    "disk10": 2,
    "disk11": 2,
    "disk24": 2,
    "disk23": 2,
    "disk03": 2,
    "PendingAlerts": 2,
    "disk04": 2,
    "disk16": 2,
    "disk22": 2,
    "disk05": 2,
    "tabladiscos": [

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/7/07dd6913e0eccbde1697a242bdbc3619c625ea16.png)

---

<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 19, 2023, 8:22am UTC](https://discuss.elastic.co/t/if-condition-loop-on-array/336518/5 "2023-07-19T08:22:47Z")

</div>

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