# Help for array index warning



## Mika31sens (Mar 14, 2014)

I have this message : 
"SCRIPT WARNING : array index out of bounds (using default range value 0) !"
I don't understand because the script works good.

Someone to help me ?

The script :
on init

declare $i
declare $j
declare $k

declare $Key_Sound := 45

declare $Num_Groups := 5
declare %Allow_Group[5] := (1,1,1,1,1)
declare %Ksw_Group[5] := (48,50,52,53,55)

declare %Ksw_Sel_Group[5] := (60,62,64,65,67)
declare $Sel_Group := 0

declare %Sel_Variation[5] := (0,0,0,0,0)
declare %Num_Variations[5] := (10,3,4,8,12)
declare $Ksw_First_Variation := 72

$i := 0
while ($i < 120)
set_key_color($i ,$KEY_COLOR_WHITE)
inc($i)
end while

set_key_color($Key_Sound,$KEY_COLOR_BLUE)

$i := 0
while ($i < ($Num_Groups+1))
set_key_color(%Ksw_Group[$i] ,$KEY_COLOR_RED)
inc($i)
end while

$i := 0
while ($i < ($Num_Groups+1))
set_key_color(%Ksw_Sel_Group[$i] ,$KEY_COLOR_GREEN)
inc($i)
end while

$i := $Ksw_First_Variation
while ($i < ($Ksw_First_Variation + %Num_Variations[$Sel_Group]))
set_key_color($i ,$KEY_COLOR_CYAN)
inc($i)
end while

set_key_color(%Ksw_Sel_Group[$Sel_Group],$KEY_COLOR_RED)

set_key_color($Ksw_First_Variation,$KEY_COLOR_RED)

end on

{***********************************************************************}

on note

{*** Allow/Disallow Instruments ***}
$i := 0
while ($i < ($Num_Groups+1))
if ($EVENT_NOTE = %Ksw_Group[$i])
if (%Allow_Group[$i] = 1)
set_key_color(%Ksw_Group[$i] ,$KEY_COLOR_YELLOW)
%Allow_Group[$i]:= 0
else
set_key_color(%Ksw_Group[$i] ,$KEY_COLOR_RED)
%Allow_Group[$i] := 1
end if
end if
if (%Allow_Group[$i] = 0)
disallow_group($i)
else
allow_group($i)
end if
inc ($i)
end while

ignore_event($EVENT_ID)
if ($EVENT_NOTE = $Key_Sound)
$i := 0
$j := 60
while ($i < ($Num_Groups))
play_note($j + %Sel_Variation[$i],$EVENT_VELOCITY,0,-1)
inc($i)
$j := $j + 12
end while
end if

{*** Select Instruments ***}
$i := 0
while ($i < ($Num_Groups+1))
if ($EVENT_NOTE = %Ksw_Sel_Group[$i])
$j := 0
while ($j < ($Num_Groups+1))
set_key_color(%Ksw_Sel_Group[$j] ,$KEY_COLOR_GREEN)
inc($j)
end while
$Sel_Group := $i
set_key_color(%Ksw_Sel_Group[$i] ,$KEY_COLOR_RED)
$k := $Ksw_First_Variation
while ($k < 120)
set_key_color($k ,$KEY_COLOR_WHITE)
inc($k)
end while
$k := $Ksw_First_Variation
while ($k < ($Ksw_First_Variation + %Num_Variations[$Sel_Group]))
set_key_color($k ,$KEY_COLOR_CYAN)
inc($k)
end while
set_key_color($Ksw_First_Variation + %Sel_Variation[$Sel_Group] ,$KEY_COLOR_RED)
end if
inc ($i)
end while

{*** Select Variations ***}
$i := 0
while ($i < (%Num_Variations[$Sel_Group]))
if ($EVENT_NOTE = $Ksw_First_Variation + $i)
$j := 0
while ($j < (%Num_Variations[$Sel_Group]))
set_key_color($Ksw_First_Variation + $j ,$KEY_COLOR_CYAN)
inc($j)
end while
%Sel_Variation[$Sel_Group] := $i
set_key_color($Ksw_First_Variation + $i ,$KEY_COLOR_RED)
end if
inc ($i)

end while
end on


----------



## andreasOL (Mar 14, 2014)

Hi 

as far as I have seen...remove the +1 from all

($Num_Groups+1)

conditions in your while loops (and then the () are redundant around $Num_Groups).

If you have $Num_Groups elements in your arrays the indices go from 0 to $Num_Groups - 1 (here: 0 to 4).

So executing the while loops up to "$i < $Num_Groups" is the right thing because then 4 is the last index.

best,
Andreas


----------



## Mika31sens (Mar 14, 2014)

o-[][]-o 
Thank you very much Andrea
After 5 hours of programming, I must be tired :roll:


----------

