What's new

Best way to write a sequential array?

unraveledagain

New Member
Is there a better, or smaller way to write a long but sequential array using math instead of writing out each number individually?

For instance, if I have an array that goes from 50 to 170, is my only option to write out 50, 51, 52, 53, and so on?
 
i'm not a KSP programmer, but have experience with other programming languages (and felt like this was a great opportunity to quickly learn some KSP)

I think this is sort of what you're attempting to do? I wrote it inside of Kontakt and it applied without error.


Code:
on init
  declare const $from := 50
  declare const $to := 170
  declare const $array_size := ($to - $from) + 1
  declare %arrayname[$array_size]

  declare $count := 0
  while ( $count < $array_size )
     %arrayname[$count] := $from + $count

      $count := $count + 1
  end while
end on

Edit: I forgot to add the increment for $count in the loop. Updated the code.
 
Last edited:
maybe this? assuming you want an integer array

for sublime
Code:
    on init
        define limit_a := 50
        define limit_b := 170
        define ARYTL := limit_b + 1 - limit_a
        declare $i
        declare %myarray[ARYTL]
        for $i := 0 to ARYTL -1
            %myarray[$i] := $i + limit_a
        end for
    end on

complies as
Code:
on init
    declare $i
    declare %myarray[121]
    $i := 0
    while ($i<121)
        %myarray[$i] := $i+50
        inc($i)
    end while
end on
 
Last edited:
Hey some really good options! Thanks a bunch, guys! I knew it would be something easy like this, but couldn't think of how to do it properly because I'm still new to this stuff.

I really appreciate it!
 
Top Bottom