Difference between revisions of "Category:LSL Negative Index"

From Second Life Wiki
Jump to navigation Jump to search
m
Line 6: Line 6:
* Index -1 is 'd' (''llGetSubString(s,-1,-1)'')
* Index -1 is 'd' (''llGetSubString(s,-1,-1)'')
* Index -5 is 'W' (''llGetSubString(s,-5,-5)'')
* 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.<br/>
Say ''n_ind'' is a negative index for ''str'' then the positive form of ''n_ind'' would be <code>n_ind + llStringLength(str);</code><br/>
Say ''p_ind'' is a positive index for ''str'' then the negative form of ''p_ind'' would be <code>p_ind - llStringLength(str);</code>




Line 18: Line 23:
     s = llDeleteSubString(s, index, index);
     s = llDeleteSubString(s, index, index);
}
}
</pre>
<div id="box" style="padding: 0.5em;float:left;">
==Range Functions==
* [[llDeleteSubString]]
* [[llGetSubString]]
* [[llDeleteSubList]]
* [[llList2List]]
* [[llListInsertList]]
* [[llListReplaceList]]
</div>
To complicate things these 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'', -1]
|}
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.
<pre style="clear: left;">
string bad(string in)
{
    return llGetSubString(in, 5, -5);
}
bad("Hello World!");//returns " Wo"
bad("Hello Cat!");//returns " C"
bad("Hello !");//returns "Hel !"
</pre>
</pre>

Revision as of 15:50, 6 March 2007

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);
}


To complicate things these 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, -1]

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"
bad("Hello Cat!");//returns " C"
bad("Hello !");//returns "Hel !"