Difference between revisions of "Category:LSL Negative Index"

From Second Life Wiki
Jump to navigation Jump to search
m
m (modernize tags)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{LSL Header|ml=*}}
{{LSL Header|ml=*}}{{LSLC{{#var:lang}}|}}
Negative indexes count backwards from the end.
Negative indexes count backwards from the end.


Line 15: Line 15:
Care must be taken when using the results of [[llListFindList]] and [[llSubStringIndex]] in combination with the functions in this category.  Those functions will return an index of -1 to indicate 'not found', which if used in these functions is a valid index - but probably not the one you want.
Care must be taken when using the results of [[llListFindList]] and [[llSubStringIndex]] in combination with the functions in this category.  Those functions will return an index of -1 to indicate 'not found', which if used in these functions is a valid index - but probably not the one you want.


<lsl>
<source lang="lsl2">
string s = "Hello World";
string s = "Hello World";


Line 23: Line 23:
     s = llDeleteSubString(s, index, index);
     s = llDeleteSubString(s, index, index);
}
}
</lsl>
</source>


==Range Functions==
==Range Functions==
Line 46: Line 46:
Things can get tricky when using mixed indexes with variable length strings and lists. The mode of ranges operated upon can switch back and forth dependent upon the length. It is important to keep this in mind when mixing indexes that count from the beginning and those that count from the end.
Things can get tricky when using mixed indexes with variable length strings and lists. The mode of ranges operated upon can switch back and forth dependent upon the length. It is important to keep this in mind when mixing indexes that count from the beginning and those that count from the end.


<lsl>string bad(string in)
<source lang="lsl2">string bad(string in)
{
{
     return llGetSubString(in, 5, -5);
     return llGetSubString(in, 5, -5);
Line 53: Line 53:
bad("Hello World!");//returns " Wo" resulting range [5, 8]
bad("Hello World!");//returns " Wo" resulting range [5, 8]
bad("Hello Cat!");//returns " " resulting range [5, 5]
bad("Hello Cat!");//returns " " resulting range [5, 5]
bad("Hello !");//returns "Hel !" resulting range [0, 2] + [5 , 6]</lsl>
bad("Hello !");//returns "Hel !" resulting range [0, 2] + [5 , 6]</source>

Latest revision as of 15:13, 22 April 2017

Negative indexes count backwards from the end.

For string s = "Hello World"

  • Index 0 is 'H' (llGetSubString(s,0,0))
  • Index -1 is 'd' (llGetSubString(s,-1,-1))
  • Index -5 is 'W' (llGetSubString(s,-5,-5))


Negative indexes are very straight forward, LSL treats negative indexes as if they were their positive counterpart.
Say n_ind is a negative index for str then the positive form of n_ind would be n_ind + llStringLength(str);
Say p_ind is a positive index for str then the negative form of p_ind would be p_ind - llStringLength(str);


Care must be taken when using the results of llListFindList and llSubStringIndex in combination with the functions in this category. Those functions will return an index of -1 to indicate 'not found', which if used in these functions is a valid index - but probably not the one you want.

string s = "Hello World";

integer index = llSubStringIndex(s, "t");//index == -1
if(~index)//same as doing (index != -1) but a bit faster, '~' inverts the bits (-1 == all on) so (~-1 == all off)
{
    s = llDeleteSubString(s, index, index);
}

Range Functions

List

String

To complicate things range functions take two indexes and operator on a range of indexes. They have start and end index parameters. Negative indexes are treated as if they are their positive counterparts.

  • If start <= end then the range of indexes the function works with are [start, end].
  • If start > end then the range of indexes the function works with is [0, end] + [start, ]

Things can get tricky when using mixed indexes with variable length strings and lists. The mode of ranges operated upon can switch back and forth dependent upon the length. It is important to keep this in mind when mixing indexes that count from the beginning and those that count from the end.

string bad(string in)
{
    return llGetSubString(in, 5, -5);
}

bad("Hello World!");//returns " Wo" resulting range [5, 8]
bad("Hello Cat!");//returns " " resulting range [5, 5]
bad("Hello !");//returns "Hel !" resulting range [0, 2] + [5 , 6]