Difference between revisions of "LlList2ListSlice"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with "{{LSL_Function |inject-2={{LSL_Function/negative index|true|start|end|noExclude=*}}{{LSL_Function/stride|stride|slice}} |func_id=198|func_sleep=0.0|func_energy=10.0 |func=llLi...")
 
Line 13: Line 13:
If {{LSLP|start}} {{LSLP|end}} or {{LSLP|slice_index}} are negative they are indexed from end of list.  -1 is last element in the list.  -list_length is the 1st element of the list.
If {{LSLP|start}} {{LSLP|end}} or {{LSLP|slice_index}} are negative they are indexed from end of list.  -1 is last element in the list.  -list_length is the 1st element of the list.
If {{LSLP|slice_index}} is negative it is counted from the end of its stride regardless of whether or not the stride exceeds the end of the list.  e.g:  -1 is the last element in a stride.
If {{LSLP|slice_index}} is negative it is counted from the end of its stride regardless of whether or not the stride exceeds the end of the list.  e.g:  -1 is the last element in a stride.
If {{LSLP|start}} < {{LSLP|end}} the range from start to end is treated as an exclusion range.
|func_desc
|func_desc
|return_text=of the slice_index'th element of every stride in [[List#Strided_lists|strided list]] whose index is a multiple of {{LSLP|stride}} in the range {{LSLP|start}} to {{LSLP|end}}.
|return_text=of the slice_index'th element of every stride in [[List#Strided_lists|strided list]] whose index is a multiple of {{LSLP|stride}} in the range {{LSLP|start}} to {{LSLP|end}}.
Line 31: Line 32:
list result_d = llList2ListSlice(mylist,2,-1,3,-1); //start at first item in list, go to the end, return last slice of every stride of 3
list result_d = llList2ListSlice(mylist,2,-1,3,-1); //start at first item in list, go to the end, return last slice of every stride of 3
//result_d == [2,5]</source>
//result_d == [2,5]</source>
list result_e = llList2ListSlice(mylist,4,2,1,0); //4>2 so this is an exclusion. slice indices count from 0 and first element after exclusion. Return every element not in range with a stride of 1
//result_d == [0,1,5,6]


<source lang="lsl2">list menu = ["1", "one", "2", "two", "3", "three"];
<source lang="lsl2">list menu = ["1", "one", "2", "two", "3", "three"];

Revision as of 15:05, 4 April 2023

Summary

Function: list llList2ListSlice( list src, integer start, integer end, integer stride, integer slice_index );
0.0 Forced Delay
10.0 Energy

Returns a list of the slice_index'th element of every stride in strided list whose index is a multiple of stride in the range start to end.

• list src
• integer start start index
• integer end end index
• integer stride number of entries per stride, if less than 1 it is assumed to be 1
• integer slice_index

This function supports Strided Lists. The index of the first entry in the list is 0
The index of the first entry in a slice is 0
If start end or slice_index are negative they are indexed from end of list. -1 is last element in the list. -list_length is the 1st element of the list. If slice_index is negative it is counted from the end of its stride regardless of whether or not the stride exceeds the end of the list. e.g: -1 is the last element in a stride. If start < end the range from start to end is treated as an exclusion range.

Specification

Index Positive Negative
First 0 -length
Last length - 1 -1

Indexes

  • Positive indexes count from the beginning, the first item being indexed as 0, the last as (length - 1).
  • Negative indexes count from the far end, the first item being indexed as -length, the last as -1.

Caveats

  • If either start or end are out of bounds the script continues to execute without an error message.
  • start & end will not form an exclusion range when start is past end (Approximately: start > end), instead it will act as if start was zero & end was -1.

Examples

list mylist = [0,1,2,3,4,5,6];
list result_a = llList2ListSlice(mylist,0,-1,3,0); //start at first item in list, go to the end, return 1st slice of every stride of 3
//result_a == [0,3,6]

list result_b = llList2ListSlice(mylist,0,-1,3,1); //start at first item in list, go to the end, return 2nd slice of every stride of 3
//result_b == [1,4]

list result_c = llList2ListSlice(mylist,1,-1,3,1); //start at second item in list, go to the end, return 2nd slice of every stride of 3
//result_c == [2,5]

list result_d = llList2ListSlice(mylist,2,-1,3,-1); //start at first item in list, go to the end, return last slice of every stride of 3
//result_d == [2,5]

list result_e = llList2ListSlice(mylist,4,2,1,0); //4>2 so this is an exclusion. slice indices count from 0 and first element after exclusion. Return every element not in range with a stride of 1 //result_d == [0,1,5,6]

list menu = ["1", "one", "2", "two", "3", "three"];
default
{
    state_entry()
    {
        llListen(10, "", llGetOwner(), "");
    }
    touch_start(integer detected)
    {
        list buttons = llList2ListSlice(menu, 0, -1, 2, 0);
        llDialog(llDetectedKey(0), "choose a number", buttons, 10); //display the digits
    }
    listen(integer channel, string obj, key id, string message)
    {
        integer index = llListFindList(menu, [message]);
        if (index != -1)
        {
            list names = llList2ListSlice(menu, 0, -1, 2, 1);
            llOwnerSay("you chose " + llList2String(names, index ) + " (" + message + ")");
        }
    }
}

Useful Snippets

  • StrideOfList - Starting from start and stopping before end exceeded it skips every stride-1 items. returns the slice_index element of the stride if its position is less than list length.

Notes

Ranges & Indexes

The easiest way to explain how ranges work is to make all indexes positive. Negative indexes are just a way of counting from the tail end instead of the beginning, all negative indexes have a corresponding equivalent positive index (assuming they are in range). Positive indexes past length (after the last index), or negative indexes past the beginning (before the first index) are valid and the effects are predictable and reliable: the entries are treated as if they were there but were removed just before output.

  • If start <= end then the range operated on starts at start and ends at end. [start, end]
  • Exclusion range: If start > end then the range operated on starts at 0 and goes to end and then starts again at start and goes to -1. [0, end] + [start, -1]
    • If end is a negative index past the beginning, then the operating range would be [start, -1].
    • If end is a positive index past the end, then the operating range would be [0, end].
    • If both start and end are out of bounds then the function would have no operating range (effectively inverting what the function is supposed to do).

See negative indexes for more information. If the list is not modulo the stride, elements up to the length of the list will be returned in the final slice matching conditions. e.g. a list of 5 elements with a stride of 2 will return 3 elements for a 0,5 range on slice index 0 but only 2 if the slice_index is 1.

See Also

Articles

•  Negative Index

Deep Notes

Signature

function list llList2ListSlice( list src, integer start, integer end, integer stride, integer slice_index );

Haiku

Ah, the strided list.
C had structs in the '70s.
Someday, we will, too...