# Array Conditions



## Raptor4 (Jul 5, 2012)

Hi,
I need some array conditioning and I wonder if that's possible in the KSP. Here is an example scenario:
I have an array[128] which has to register values 0 and 1 only - i.e values 0 & 1 are stored in the array only. Let say that all array position values are set to 0 and only array positions 36, 40 & 43 are set to 1. 
How can I check up the 128 array positions in one go and process only the ones which use value 1 - i.e array positions 36, 40 & 43 which are previously set to value 1 in the array? Using "If" 128 times for checking up is not an option (it is known that it should work). I'm looking for more optimized complex way.
Thanks in advance!


----------



## Reegs (Jul 5, 2012)

Use a loop to iterate through:

My KSP is a little rusty so please bear with my pseudocode:


```
declare i := 0
while (i < 128)
        if (array[i] = 1)
            do stuff
        end if
    i := i + 1
end while
```


----------



## Big Bob (Jul 5, 2012)

Another possibility exists if:

1. There are only a few ones actually on
2. You can reset the one to zero right after you take action.

For that situation, you could use the search function which would then execute a lot faster than looping through all 128 elements. For example something like this:


```
i := search(Array,1)
  while i # -1
    { process index position i }
    Array[i] := 0
    i := search(Array,1)
  end while
```

Rejoice,

Bob


----------



## Raptor4 (Jul 6, 2012)

Thanks for all replies guys!
Originally I was thinking about using a "loop" checking and even about the array search. Excuse me if I'm wrong in the understanding that (I'm new in the ksp), but such kinds of or "continuous" checking results next:
When the loop is started the first "met" event which matches the condition gives you the possibility to use only one operation. How do you use another operation for the second matching event etc ?
Here is the main point in my OP again:


> How can I check up the 128 array positions in one go and process only the ones which use value 1 - i.e array positions 36, 40 & 43 which are previously set to value 1 in the array?


To my opinion we need some kind of second condition which determines the array position #. Here is a simple demo using multiple "if" condition and "fixed" array position # (in my real scenarios the array pos # can be unknown).

```
on init
declare ui_switch $Check
declare ui_switch $A
declare ui_switch $B
declare ui_switch $C
declare ui_table %table[10](5, 4, 1) 
end on

on ui_control($Check)
if (%table[2]=1)
$A := $Check
end if
if (%table[4]=1)
$B := $Check
end if
if (%table[6]=1)
$C := $Check
end if
end on
```

Just an idea (I'll try to find some time to try it tonight).
Let's say we create another "Temp" array where we can temporary store the matching events from the original array but in logical order as array positions 0,1,2,3 etc so we can easily "loop/bang" the "Temp" array later, something like that...
Regards,

R4


----------



## mk282 (Jul 6, 2012)

Big Bob's solution is pretty good. Endless ifs are not really good here, both from coding and efficiency perspective.


----------



## Reegs (Jul 6, 2012)

Yeah, I like Big Bob's method too unless you need to preserve the values.

The alternative is that would work with it is, as you say, to create a temporary storage array to hold the values, although once you're through with 

```
array search on Ary - find i
  push index i to TempAry1
  push val i to TempAry2
  operate on pos i
  set Ary[i] =0
repeat
for each nonzero in TempAry1 
  pop val to Ary[i]
```

I'm not so sure if a simple loop over 128 elements, which is pretty small, wouldn't be more efficient. Probably worth a test.

But you're looking to match the n occurrences of a 1 in the table array to individual variables, using a new variable each time there's a match? There's probably a better way to approach that if you can give us a few more specifics.


----------



## Big Bob (Jul 6, 2012)

In my posted example, note the comment following the while condition, namely:

```
{ process index position i }
```
I couldn't illustrate this because you haven't disclosed enough about the kind of processing 'action' you want to perform.

However, it seems to me that whatever it is, it should be reduceable to a function whose action depends solely on the index i for each loop pass that discovers an On condition.

In the worst general case, i could be used as a select-case index with each case devoted to some possible unique action. However, I doubt that you actually want to implement 128 different actions. Again, without you disclosing more detail about what action or actions you need to take, as Reegs has already said, it's difficult to offer any more-specific help.

Rejoice,

Bob


----------



## Raptor4 (Jul 6, 2012)

Thanks for the detail replies guys !
I'm doing some tests using note events for example (I have working schemes using multiple "If" which work fine) but I want to optimize the multiple "If" conditions with some more elegant scheme.
Let's say I want to make something like short "note event history". Regarding my OP example let's say that C1, E1 & G1 are played and are registered as array positions 36, 40 & 43 using value 1 (yes). To process these note events later I need at least two matching conditions - the float event position ID (like 36,40 & 43) and the register value condition =1.
Here is my working prototype where I use two arrays and multiple "If" so this is just one string of the scheme:

```
if (%note_reg[60]=1)
note_off(%note_id[60])
end if
```

Regards
R4


----------



## mk282 (Jul 6, 2012)

Why aren't you using %KEY_DOWN variable? It holds the states of pressed keys...


----------



## Raptor4 (Jul 6, 2012)

> Why aren't you using %KEY_DOWN variable? It holds the states of pressed keys...


Thanks for your time mk282, but I'm not looking for alternatives - it was just an example. Regarding the topic label I'm looking for array conditions which detect the pos # ID and the pos # value.


----------



## Big Bob (Jul 6, 2012)

> Regarding the topic label I'm looking for array conditions which detect the pos # ID and the pos # value.



The way I read your original post, you were dealing with an array populated only with zeros or ones and you were trying to find the indices for each slot holding a one. Now you are saying the array can contain other values as well?

Me thinks maybe you need to ask your questions more accurately :lol:


----------



## Raptor4 (Jul 6, 2012)

> Now you are saying the array can contain other values as well?


I'm sorry Bob if I could not be so detailed ! As a whole I'm talking about two array registrations from the beginning regarding my example - _"36, 40 & 43 which are previously set to value 1 in the array"_ where 36, 40 & 43 are the "pos ID # " and the value=1 setting is the "pos value". Later I gave some examples using multiple "if" conditions which I want to replace. Forgive me if I'm wrong in the explanation but my expectations was oriented to the extra midi conditions which require at least two conditions... o/~


----------



## Big Bob (Jul 6, 2012)

Sorry but I guess I'm still not clear on what you are trying to do. :? Your latest post now translates into something like:

You have two arrays, one with a few flags set but otherwise full of zeros. At any array *position* where a flag is set (ie the location contains *one* instead of *zero*), you then want to do something with the data at the corresponding index position in the 2nd array? 

But that can't really be what you want because otherwise my prior suggestion would handle it. So, I'm afraid that you still haven't stated the problem clear enough for me to understand it, but, maybe one of the other respondents can follow what you are saying better than I can. :roll: 

But, I wish you the best. 

Bob


----------



## Raptor4 (Jul 7, 2012)

> At any array position where a flag is set (ie the location contains one instead of zero), you then want to do something with the data at the corresponding index position in the 2nd array?


Definitely !
Hey Bob do you know that you are GOD ? o-[][]-o 
I'm sorry I was quite tired yesterday and could not get your original idea in 100%.
Today I re-read that again and have to say that it is a genius solution!!! 
You opened the Eden gates for me dude! I'm sorry for my temporary misunderstanding!
THANKS A LOT !

R4 o=?

BTW: Thanks to everybody who assist me in this topic as well !


----------



## mk282 (Jul 7, 2012)

Toldya Big Bob's solution was the best


----------



## Big Bob (Jul 7, 2012)

> Hey Bob do you know that you are GOD ?



Oh my no, I'm definitely not GOD but, I am very blessed by Him.

In any case, I'm glad we were able to help 8) 

Rejoice,

Bob


----------

