# Counter problem for specific file types

**URL:** <https://www.ruby-forum.com/t/counter-problem-for-specific-file-types/174877>\
**Category:** Ruby\
**Created:** [September 3, 2009, 11:58am UTC](https://www.ruby-forum.com/t/counter-problem-for-specific-file-types/174877 "2009-09-03T11:58:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 3, 2009, 11:58am UTC](https://www.ruby-forum.com/t/counter-problem-for-specific-file-types/174877/1 "2009-09-03T11:58:12Z")

</div>

Hi all,

This is a real teaser for me and would appreciate some help with the  
logic. I want to introduce some numbering to a data set. Basically I  
have two sets of items the,a control item and a process item. The  
control item is always assigned the number 0 eg. if type == “Control”  
number = “0” end

A control item may or may not have process, and may have multiple  
processes. I have a while loop that goes through every single item and  
when it encounters a Control item it assigns it a 0, what I want it to  
do also is assign a continuous number to each process item.

Take this algorithm

if type = “control”  
number = 0  
elsif type = “process”  
number = counter starting at one  
end

It will keep adding a continuous number to each process item until it  
comes across a type of control or something else. So you will end up  
with an output like this

control, 0  
process, 1  
process, 2  
process, 3,  
control, 0  
process, 1  
something else, NULL  
control, 0  
control, 0  
process, 1

and so on.

I hope I am making sense with this, remember this code is in a while  
loop going through each individual item.

Any input to get started on this is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 3, 2009, 12:45pm UTC](https://www.ruby-forum.com/t/counter-problem-for-specific-file-types/174877/2 "2009-09-03T12:45:16Z")

</div>

On Thu, Sep 3, 2009 at 11:58 AM, Ne  
Scripter[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> when it encounters a Control item it assigns it a 0, what I want it to  
> It will keep adding a continuous number to each process item until it  
> comes across a type of control or something else.

If I understood correctly, what you can do is to have a counter that  
starts at 1, and it’s reset every time you come across a “control” or  
that “something else you mention”:

counter = 1  
if type == “control”  
number = 0  
counter = 1  
elsif type == “process”  
number = counter  
counter += 1  
end

Hope this helps,

Jesus.
