# structure with counter

Okay, I'm going to try to explain this as best as possible.

I have a structure list set up and a counter in place to step forward/backwards amongst the structure, I have the composition set up to go back to the beginning of the structure list once the increase signal is sent and the count reaches above the structure count. My question is this, how can I get the count to go to the end of the structure if decrease signal is sent to the the count and the count is at 0?

## Comment viewing options

### Re: structure with counter

One possible path to enlightenment, Ninja, involves breaking free of the `Counter` patch, for it is only capable of outputting nonnegative values and therefore clamps to zero.

Consider the attached example.

PreviewAttachmentSize
WrappingCounter.qtz4.74 KB

### Re: structure with counter

Elegant, smokris.

I started making a simple MathExp solution, too, but ran into the clamp to zero problem. So I did what I usually do, JS'ed my way out of it.

In the interest of completeness, here it is:

```var result;

function (__number Count) main (__boolean Increase, __boolean Decrease, __boolean Reset, __number Maximum)
{

// init:
if(_testMode) {
result = new Object();
result.Count = 0;
}
// run
else {

if( Increase ) {
result.Count++;
if( result.Count > Maximum ) {
result.Count = 0;
}
}
else if( Decrease ) {
result.Count--;
if( result.Count < 0 ) {
result.Count = Maximum;
}
}

if( Reset ) {
result.Count = 0;
}

return result;

}
}```

Probably more expensive that way. Oh, BTW, this has a sort of hacky edge detection built in - it'll only update when the inputs change.

ETA: I didn't make the math expression work for myself since I was stumped for a "store variable" block like some other node systems have (like Reaktor). For me, the most natural thing would be to think of the variable I want to store as an object itself.

If I understand your patch correctly, it depends on a feedback loop between two math expressions, which keeps the value in memory, right? It wouldn't work if you combined them. It's elegant, but a bit obscure.

BTW, the branching math exp is cool - my first impulse was to use % and abs, but after noodling around I see that doesn't cut it.

### Re: structure with counter

Thank you smokris,

I never considered using the pulse patch with math expression, this will be very useful in quite a few of my compositions, Im currently working on a timeline presenter and going from beginning straight to the end was kicking me.

Thanks again

P.S. the path to enlightenment appears to be within reach. :P