# Iteration In Logstash | Looping

**URL:** <https://discuss.elastic.co/t/iteration-in-logstash-looping/296148>\
**Category:** Logstash\
**Created:** [February 3, 2022, 6:14am UTC](https://discuss.elastic.co/t/iteration-in-logstash-looping/296148 "2022-02-03T06:14:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mayank\_Malhotra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayank_malhotra/32/91991_2.png) [@Mayank\_Malhotra](https://discuss.elastic.co/u/Mayank_Malhotra)\
**Post date:** [February 3, 2022, 6:14am UTC](https://discuss.elastic.co/t/iteration-in-logstash-looping/296148/1 "2022-02-03T06:14:24Z")

</div>

Hi All,  
I am creating a logstasg config in which I need to use iteration at some point giving an example below in terms of simpler programing language :-

```auto
int n=300000;
int counter=0,skip=0;
while(counter<300000)
{
skip=counter;
counter+=5000;
}

```

I need to increase skip by 5000 every time and store different values of it until it reaches n.  
I tried to create a ruby code below in my logstash config, it is adding 5000 but how to iterate it that's the challenge:-

```auto
ruby{
	init => "@counter = 0
			@max_count = 0"
		code => '
			@max_count = event.get("total_records");
			event.set("skip",@counter)
			if @counter < @max_count
			@counter = @counter + 5000;
			end
		'
	}

```

If someone can guide then it would be a great help!  
Thanks in Advance!

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 3, 2022, 6:59am UTC](https://discuss.elastic.co/t/iteration-in-logstash-looping/296148/2 "2022-02-03T06:59:52Z")

</div>

Using Aggregation Filter Plugin, you can hold variables across events.

> **[Aggregate filter plugin | Logstash Reference \[7.17\] | Elastic](https://www.elastic.co/guide/en/logstash/current/plugins-filters-aggregate.html#plugins-filters-aggregate-code)**

---

<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:** [March 3, 2022, 7:00am UTC](https://discuss.elastic.co/t/iteration-in-logstash-looping/296148/3 "2022-03-03T07:00:34Z")

</div>

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