Welcome to %s forums

BrainModular Users Forum

Login Register

FastScript - Find index of smallest number in an Array

I need help on a Patch
Post Reply
sephult
Member
Posts: 1144
Contact:

Unread post by sephult » 08 Mar 2015, 16:33

Hello!

So I have run into a problem at the moment I cannot wrap my head around.

I need to find the index of the smallest number in an Array, I know in several programming languages there are functions to do this.
I thought this was quite an easy thing to do and I have done before in other languages, however I am starting to become stumped (probably just finding a downfall of pascal??).
Definitely wasn't expecting to get hung-up by this.

So below my solution was a simple conditional check within a For loop. If smaller than the variable then make the variable equal to the iteration/index.
Keep checking for smaller numbers. However, the result of oldest is always Zero even when the smallest number exists in [1].

So Process procedure decides and receives the event trigger, if a condition exists secondary procedures are executed which contain this code below.
The variable oldest is a value from 1 to 16.

Anyone have a clue as to why this may be, or have some guidance to an alternative method to perform this?

Code: Select all

  oldest:=17; //Initialize for detecting oldest index           
 
  //Search for lowest number in array and purge
              
  for k:=0 to 15 do begin   
          
    if order_array&#91;k&#93; < oldest then begin            
      oldest &#58;= k;                       
    end;
end;


-S
"Every act of creation is first an act of destruction." -Picasso

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 09 Mar 2015, 08:31

You mix up two things here; the values to check, and the array index.

You could do eg:

Code: Select all

oldest &#58;= 0;

FOR k &#58;= 1 TO &#40;ARRAY_LENGTH - 1&#41; DO BEGIN
  IF &#40;order_array&#91;k&#93; < order_array&#91;oldest&#93;&#41; THEN
     oldest &#58;= k;
END;
Bjørn S

sephult
Member
Posts: 1144
Contact:

Unread post by sephult » 09 Mar 2015, 13:35

Awesome thanks for the guidance bsork!

I was getting totally stumped for a moment. Below is the code that is working for me now.
Was there any reason why you started the array index as 1, or was this just a mistake? Had me curious.

Code: Select all

  oldest&#58;=0; //Initialize for detecting oldest index           
              
  for k&#58;=0 to getarraylength&#40;order_array&#41;-1 do begin                                      
          
    if &#40;order_array&#91;k&#93; < order_array&#91;oldest&#93;&#41; then           
      oldest &#58;= k;                       
                                               
        
  end;
Again, Thank you Bsork!!

Best Regards,

-S
"Every act of creation is first an act of destruction." -Picasso

bsork
Site Admin
Posts: 1334
Location: Asker, Norway
Contact:

Unread post by bsork » 09 Mar 2015, 14:42

I startet at 1 as we are comparing pairs: When k is 1, oldest is always 0, when k is 2, oldest is 0 or 1 depending on the previous test, etc...

It looks a bit more obvious (to me, at least) if the variable name is changed from "oldest" to "lowest" or something similar.
Bjørn S

sephult
Member
Posts: 1144
Contact:

Unread post by sephult » 09 Mar 2015, 15:50

Bsork,

Yes I agree that the name should be changed from oldest to lowest for a generic.

In my specific script I created a Dynamic Channelization, so for the procedure I am trying to actually determine the note that has stayed in the array the longest.
If all channel slots are empty, I purge the oldest one to make room for the next.

It is actually working quite nice now, I have been working on implementing scala tuning into Hollyhock. As of now, I have a format that does direct Frequency output, I am now working on the MIDI pitchbend. Once finished my goal is to provide Alternate tunings not only for single notes, but for chords.

-S
"Every act of creation is first an act of destruction." -Picasso

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests