KSPリファレンスマニュアル 9 - 配列コマンド(Array Commands)

KSPリファレンスマニュアル 9 - 配列コマンド(Array Commands)


array_equal()

array_equal(<array-variable>, <array-variable>)

2つの配列の値どうしを比較します。すべての値が等しい場合は1を返し、等しくない場合は0を返します。

補足

  • このコマンドは、実数を格納した配列に対しては動作しません。

on init
    declare %array_1[10]
    declare %array_2[11]

    if (array_equal(%array_1, %array_2))
        message("Arrays are not equal!")
    else
        message("Arrays are equal!")
    end if
end on

2つの配列のサイズが同じではないため、このスクリプトを実行するとエラーメッセージが表示されます。

関連項目

sort()

num_elements()

search()


num_elements()

num_elements(<array-variable>)

配列に含まれる要素の数を返します。

補足

  • この関数を使うと、例えばnum_elements(%GROUPS_AFFECTED)のように書いて、現在のイベントが影響を与えるグループの数を調べることができます。

on note
    message(num_elements(%GROUPS_AFFECTED))
end on

キーを押したときに再生されているグループの数を出力します。

関連項目

array_equal()

sort()

search()

イベントとMIDI: %GROUPS_AFFECTED


search()

search(<array-variable>, <value>)

search(<array-variable>, <value>, <from>, <to>)

指定した配列の中から指定した値を検索し(<from><to>で範囲を指定して、その範囲内だけを対象にすることもできます)、最初に見つかった位置のインデックスを返します。値が見つからなかった場合、この関数は-1を返します。

<array-variable>

検索の対象となる配列です。

<value>

指定した配列の中から探し出す値です。

<from>

検索処理を開始する位置となる配列インデックスを指定する、省略可能な引数です。

<to>

検索処理を終了する位置となる配列インデックスを指定する、省略可能な引数です。

補足

  • このコマンドは、実数を格納した配列に対しては動作しません。

on init
    declare ui_table %array[10] (2, 2, 5)
    declare ui_button $check

    set_text($check, "Zero present?")
end on

on ui_control ($check)
    if (search(%array, 0) = -1)
        message("No")
    else
        message("Yes")
    end if

    $check := 0
end on

特定の値が配列の中に存在するかどうかを確認します。

on init
    declare const $SEARCH_FOR  := 54321
    declare const $SEARCH_FROM := 54000
    declare const $SEARCH_TO   := 55000

    declare $i
    declare %array[100000]

    declare ui_button $Check

    set_text($Check, $SEARCH_FOR & " present?")

    { fill the array with sequential numbers, just to have something to search through }
    while ($i < num_elements(%array))
        %array[$i] := $i

        inc($i)
    end while
end on

on ui_control ($Check)
    if (search(%array, $SEARCH_FOR, $SEARCH_FROM, $SEARCH_TO) = -1)
        message("No")
    else
        message("Yes")
    end if

    $Check := 0
end on

大きな配列の中の限られた範囲だけを対象に、特定の値を検索します。KSPでwhileループを使って同じことを直接行うよりも、はるかに高いパフォーマンスが得られます。

関連項目

array_equal()

num_elements()

sort()


sort()

sort(<array-variable>, <direction>)

sort(<array-variable>, <direction>, <from>, <to>)

配列を昇順または降順に並べ替えます(<from><to>で範囲を指定して、その範囲内だけを対象にすることもできます)。

<array-variable>

並べ替えの対象となる配列です。

<direction>

0に等しい場合、配列は昇順に並べ替えられます。

0に等しくない場合、配列は降順に並べ替えられます。

<from>

並べ替え処理を開始する位置となる配列インデックスを指定する、省略可能な引数です。

<to>

並べ替え処理を終了する位置となる配列インデックスを指定する、省略可能な引数です。

on init
    declare $count

    declare ui_button $Invert
    declare ui_table %array[128] (3, 3, 127)

    while ($count < 128)
        %array[$count] := $count

        inc($count)
    end while
end on

on ui_control ($Invert)
    sort(%array, $Invert)
end on

直線的なカーブ表示を素早く反転させます。

on init
    declare const $ARRAY_SIZE := 32

    declare ui_table %T[$ARRAY_SIZE](6, 4, -100)
    declare ui_value_edit $From (1, $ARRAY_SIZE, 1)
    declare ui_value_edit $To (1, $ARRAY_SIZE, 1)
    declare ui_button $Randomize
    declare ui_button $Direction
    declare ui_button $SortAll
    declare ui_button $SortRange

    make_persistent(%T)
    make_persistent($From)
    make_persistent($To)

    $From := 4
    $To := 8
end on

on ui_control ($Randomize)
    $i := 0
    while ($i < num_elements(%T))
        %T[$i] := random(-100, 100)

        inc($i)
    end while

    $Randomize := 0
end on

on ui_control ($SortAll)
    sort(%T, $Direction)

    $SortAll := 0
end on

on ui_control ($SortRange)
    sort(%T, $Direction, $From, $To)

    $SortRange := 0
end on

配列全体を並べ替えた場合と、配列内の一定の範囲だけを並べ替えた場合とを比較します。

関連項目

array_equal()

num_elements()

sort()


参照元情報:Array Commands
https://docs.native-instruments.com/online-guides/ksp-manual/en/array-commands