Difference between revisions of "LlOrd"

From Second Life Wiki
Jump to navigation Jump to search
(Added index notes)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{LSL Function
{{LSL Function
|inject-2={{LSL_Function/negative index|true|index}}
|func_sleep=0.0|func_energy=10.0
|func_sleep=0.0|func_energy=10.0
|func=llOrd|sort=Ord
|func=llOrd|sort=Ord
Line 9: Line 10:
|p2_hover=Index of character ordinal to retrieve.
|p2_hover=Index of character ordinal to retrieve.
|return_text=
|return_text=
|func_footnote=The returned value is the UTF-32 value of the character at the specified index.
|func_footnote=The returned value is the UTF-32 value of the character at the specified index.  If index is outside the bounds of the string, this function returns 0.
|spec
|spec
|caveats
|caveats
|examples=
|examples=
<source lang="lsl2">
default
{
    touch_start(integer total_number)
    {
        string test_string = "The quick brown fox jumped over the lazy dog";
        list test_list = [];
        string test_string2 = "";
       
        integer index;
        integer ord;
        for (index = 0; index < llStringLength(test_string); ++index)
        {
            ord = llOrd(test_string, index);
            test_list = test_list + [ ord ];
        }
       
        string char;
        for (index = 0; index < llGetListLength(test_list); ++index)
        {
            ord = llList2Integer(test_list, index);
            char = llChar(ord);
            test_string2 = test_string2 + char;
        }
       
        llSay(0, "\"" + test_string + "\" -> [" +
            llDumpList2String(test_list, ", ") + "] -> \"" + test_string2 + "\"");
    }
}
</source>
|helpers|related
|helpers|related
|also_functions=
|also_functions=

Latest revision as of 09:30, 14 May 2021

Summary

Function: integer llOrd( string val, integer index );
0.0 Forced Delay
10.0 Energy

Calculate the ordinal value for a character in a string.
Returns an integer

• string val Source string for character ordinal.
• integer index Index of character ordinal to retrieve.

index supports negative indexes. The returned value is the UTF-32 value of the character at the specified index. If index is outside the bounds of the string, this function returns 0.

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 index is out of bounds the script continues to execute without an error message.

Examples

default
{
    touch_start(integer total_number)
    {
        string test_string = "The quick brown fox jumped over the lazy dog";
        list test_list = [];
        string test_string2 = "";
        
        integer index;
        integer ord;
        for (index = 0; index < llStringLength(test_string); ++index)
        {
            ord = llOrd(test_string, index);
            test_list = test_list + [ ord ];
        }
        
        string char;
        for (index = 0; index < llGetListLength(test_list); ++index)
        {
            ord = llList2Integer(test_list, index);
            char = llChar(ord);
            test_string2 = test_string2 + char;
        }
        
        llSay(0, "\"" + test_string + "\" -> [" + 
            llDumpList2String(test_list, ", ") + "] -> \"" + test_string2 + "\"");
    }
}

See Also

Functions

• llChar Convert an ordinal into a character
• llHash Calculate a 32bit hash for a string

Articles

•  Negative Index
•  "Wikipedia logo"UTF-32

Deep Notes

Signature

function integer llOrd( string val, integer index );