Skip to main content

Array Commands

array_equal()

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

Checks the values of two arrays. Returns 1 if all values are equal, 0 if not.

Remarks

  • This command does not work with arrays of real numbers.

Examples

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

This script will produce an error message as the two arrays don't have the same size.

See Also

sort()

num_elements()

search()

num_elements()

num_elements(<array-variable>)

Returns the number of elements in an array.

Remarks

  • With this function you can, e.g., check how many groups are affected by the current event, using num_elements(%GROUPS_AFFECTED).

Examples

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

Outputs the number of groups that are playing when you press a key.

See Also

array_equal()

sort()

search()

Events and MIDI: %GROUPS_AFFECTED

search()

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

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

Searches the specified array for the specified value (optionally within the range specified by <from> and <to>) and returns the index of its first position. If the value is not found, the function returns -1.

<array-variable>

Array to be searched through

<value>

Value to be found in the specified array.

<from>

Optional argument which specifies the array index from which to start the searching operation.

<to>

Optional argument which specifies the array index at which searching operation will end.

Remarks

  • This command does not work with arrays of real numbers.

Examples

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

Checking if a specific value is present in an array.

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

Searching for a specific value in a smaller part of a large array - much more performant than doing the same thing with a while loop directly in KSP!

See Also 

array_equal()

num_elements()

sort()

sort()

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

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

Sorts an array in ascending or descending order (optionally within the range specified by <from> and <to>).

<array-variable>

The array to be sorted.

<direction>

When equal to 0, the array is sorted in ascending order.

When not equal to 0, the array is sorted in descending order.

<from>

Optional argument which specifies the array index from which to start the sorting operation.

<to>

Optional argument which specifies the array index at which sorting operation will end.

Examples

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

Quickly inverting a linear curve display.

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

Comparing sorting the whole array versus sorting a range within the array.

See Also 

array_equal()

num_elements()

sort()