Iteration In Logstash | Looping

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 :-

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:-

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!

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