Difference between revisions of "LlListFindList"

From Second Life Wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Issues/SVC-5146}}{{LSL_Function
{{LSL_Function
|inject-2={{Issues/SVC-5146}}{{Issues/BUG-10924}}
|func=llListFindList
|func=llListFindList
|func_id=201|func_sleep=0.0|func_energy=10.0
|func_id=201|func_sleep=0.0|func_energy=10.0
|return_type=integer|p1_type=list|p1_name=src|p2_type=list|p2_name=test
|return_type=integer
|func_footnote=If '''test''' is not found in '''src''', {{HoverText|-1|negative one, 0x{{LSL_Hex/Write|-1}}}} is returned.<br/>
|Return_text=index of the first instance of {{LSLP|test}} in {{LSLP|src}}.
|p1_type=list|p1_name=src|p1_desc=what to search in (haystack)
|p2_type=list|p2_name=test|p2_desc=what to search for (needle)
|func_footnote=If {{LSLP|test}} is not found in {{LSLP|src}}, -1 is returned.<br/>
The index of the first entry in the list is {{HoverText|0|zero}}<br>
The index of the first entry in the list is {{HoverText|0|zero}}<br>
If '''test''' is found at the last index in '''src''' the positive index is returned (5th entry of 5 returns 4).
If {{LSLP|test}} is found at the last index in {{LSLP|src}} the positive index is returned (5th entry of 5 returns 4).
|func_desc
|func_desc
|return_text=that is the index of the first instance of '''test''' in '''src'''.
|spec
|spec
|caveats=
|caveats=
Line 16: Line 19:
** "a822ff2b-ff02-461d-b45d-dcd10a2de0c2" != (key)"a822ff2b-ff02-461d-b45d-dcd10a2de0c2"
** "a822ff2b-ff02-461d-b45d-dcd10a2de0c2" != (key)"a822ff2b-ff02-461d-b45d-dcd10a2de0c2"
** "Justice" != "justice"
** "Justice" != "justice"
* If '''test''' is an {{HoverText|empty list|[]}} the value returned is {{HoverText|0|zero}} rather than {{HoverText|-1|negative one}}.
* If {{LSLP|test}} is an {{HoverText|empty list|[]}} the value returned is {{HoverText|0|zero}} rather than -1.
|constants
|constants
|examples=<lsl>list numbers = [1, 2, 3, 4, 5];
|examples=<source lang="lsl2">list numbers = [1, 2, 3, 4, 5];
default
default
{
{
Line 31: Line 34:
         }
         }
     }
     }
}</lsl>
}</source>
<lsl>//You can also search for two items at once to find a pattern in a list
<source lang="lsl2">//You can also search for two items at once to find a pattern in a list
list avatarsWhoFoundMagicLeaves = ["Fire Centaur","Red Leaf"];
list avatarsWhoFoundMagicLeaves = ["Fire Centaur","Red Leaf"];
default
default
Line 46: Line 49:
         }
         }
     }
     }
}</lsl>
}</source>
|helpers=
|helpers=
An easy way to see if an item exists in a list...
An easy way to see if an item exists in a list...
<lsl>if(~llListFindList(myList, (list)item))
<source lang="lsl2">if(~llListFindList(myList, (list)item))
{//it exists
{//it exists
     //This works because ~(-1) == 0
     // This works because ~(-1) produces 0, but ~ of any other value produces non-zero and causes the 'if' to succeed
     //It saves bytecode and is faster then doing != -1
    // So any return value (including 0) that corresponds to a found item, will make the condition succeed
     //This is a bitwise NOT (~) not a negation (-)
     // It saves bytecode and is faster then doing != -1
}</lsl>
     // This is a bitwise NOT (~) not a negation (-)
}</source>
|also_functions=
|also_functions=
{{LSL DefineRow||[[llSubStringIndex]]|Find a string in another string}}
{{LSL DefineRow||[[llSubStringIndex]]|Find a string in another string}}
Line 61: Line 65:
|also_articles
|also_articles
|notes
|notes
|permission
|negative_index
|sort=ListFindList
|sort=ListFindList
|cat1=List
|cat1=List
|haiku={{Haiku|Where are my car keys?|Has anyone seen my keys?|Oh! In my pocket.}}
|cat2
|cat2
|cat3
|cat3
|cat4
|cat4
}}
}}

Latest revision as of 04:04, 16 December 2015

Summary

Function: integer llListFindList( list src, list test );
0.0 Forced Delay
10.0 Energy

Returns the integer index of the first instance of test in src.

• list src what to search in (haystack)
• list test what to search for (needle)

If test is not found in src, -1 is returned.
The index of the first entry in the list is 0
If test is found at the last index in src the positive index is returned (5th entry of 5 returns 4).

Caveats

  • Strict type matching and case sensitivity is enforced.
    • "1" != 1
    • "1.0" != 1.0
    • 1 != 1.0
    • "a822ff2b-ff02-461d-b45d-dcd10a2de0c2" != (key)"a822ff2b-ff02-461d-b45d-dcd10a2de0c2"
    • "Justice" != "justice"
  • If test is an empty list the value returned is 0 rather than -1.

Examples

list numbers = [1, 2, 3, 4, 5];
default
{
    state_entry()
    {
        integer index = llListFindList(numbers, [3]);
        if (index != -1)
        {
            list three_four = llList2List(numbers, index, index + 1);
            llOwnerSay(llDumpList2String(three_four, ","));
            // Object: 3,4
        }
    }
}
//You can also search for two items at once to find a pattern in a list
list avatarsWhoFoundMagicLeaves = ["Fire Centaur","Red Leaf"];
default
{
    state_entry()
    {
        integer index = llListFindList(avatarsWhoFoundMagicLeaves, ["Fire Centaur","Red Leaf"]);
        if (index != -1)
        {
            list output = llList2List(avatarsWhoFoundMagicLeaves, index, index + 1);
            llOwnerSay(llDumpList2String(output, ","));
            // Object: Fire Centaur, Red Leaf
        }
    }
}

Useful Snippets

An easy way to see if an item exists in a list...

if(~llListFindList(myList, (list)item))
{//it exists
    // This works because ~(-1) produces 0, but ~ of any other value produces non-zero and causes the 'if' to succeed
    // So any return value (including 0) that corresponds to a found item, will make the condition succeed
    // It saves bytecode and is faster then doing != -1
    // This is a bitwise NOT (~) not a negation (-)
}

See Also

Functions

•  llSubStringIndex Find a string in another string

Deep Notes

Signature

function integer llListFindList( list src, list test );

Haiku

Where are my car keys?
Has anyone seen my keys?
Oh! In my pocket.