Like

From Second Life Wiki
Revision as of 10:05, 13 July 2008 by Chaz Longstaff (talk | contribs) (New page: {{LSL Header}} __NOTOC__ <div id="box"> == Function: string like(string {{LSL Param|arg1}}, string {{LSL Param|arg2}}); == <div style="padding: 0.5em;"> See if one word matches...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Function: string like(string arg1, string arg2);

See if one word matches part of another.

To use it, you pass the function a value and a mask.

Examples:

like("Susie", "Sus%") will return true, for any value starting with "Sus"
like("Susie", "%Sus%") will return true, for any value containing "Sus"
like("Susie", "%Sus") will return false. This example is looking for a string ending in "Sus".
like("Susie", "Sus") will return false. This example is looking for a string matching only "Sus" exactly.

You can, for example, loop through a list, passing it extracted values from the list and the mask, and it will let you pull out items from the list which are a valid match according to the mask criteria you specified.

Specification

<lsl> integer like(string arg1, string arg2) {

       float tmpx; float tmpy;
       if(llGetSubString(arg2,0,0)=="%"){
           arg2=llGetSubString(arg2,1,-1);tmpy=1;
       }
       if(llGetSubString(arg2,llStringLength(arg2)-1,-1) == "%") {
           arg2=llGetSubString(arg2,0,llStringLength(arg2)-2);tmpy=tmpy+2;
       }
       if(-1!=(tmpx=llSubStringIndex(arg1,arg2))) {
           if((tmpy==0 && llStringLength(arg1)==llStringLength(arg2))
           ||(tmpy==1 && tmpx == (llStringLength(arg1)- llStringLength(arg2)))
           ||(tmpy==2 && tmpx == 0)
           ||(tmpy==3)) {
                 return TRUE;
           }
       }
       return FALSE;

}


</lsl>

Posted here with the kind permission of Very Keynes. Originally posted February 2008 at http://forums.secondlife.com/showthread.php?t=243445 .