Counter problem for specific file types

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.

On Thu, Sep 3, 2009 at 11:58 AM, Ne
Scripter[email protected] 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs