Difference between revisions of "Like"

From Second Life Wiki
Jump to navigation Jump to search
m
m (<lsl> tag to <source>)
 
Line 10: Line 10:
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.
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.
|examples=
|examples=
<lsl>like("Susie", "Sus%");  //will return true, for any value starting with "Sus"
<source lang="lsl2">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 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 ending in "Sus".
like("Susie", "Sus");  //will return false. This example is looking for a string matching only "Sus" exactly.</lsl>
like("Susie", "Sus");  //will return false. This example is looking for a string matching only "Sus" exactly.</source>
|spec=<lsl>integer like(string value, string mask) {
|spec=<source lang="lsl2">integer like(string value, string mask) {
     integer tmpy = (llGetSubString(mask,  0,  0) == "%") |  
     integer tmpy = (llGetSubString(mask,  0,  0) == "%") |  
                   ((llGetSubString(mask, -1, -1) == "%") << 1);
                   ((llGetSubString(mask, -1, -1) == "%") << 1);
Line 29: Line 29:
     }
     }
     return FALSE;
     return FALSE;
}</lsl>
}</source>
|helpers
|helpers
|also_functions
|also_functions

Latest revision as of 15:27, 24 January 2015

Summary

Function: integer like( string value, string mask );

See if one word matches part of another.
Returns an integer that is TRUE if value tested positive for mask, otherwise it is FALSE.

• string value
• string mask

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

integer like(string value, string mask) {
    integer tmpy = (llGetSubString(mask,  0,  0) == "%") | 
                  ((llGetSubString(mask, -1, -1) == "%") << 1);
    if(tmpy)
        mask = llDeleteSubString(mask, (tmpy / -2), -(tmpy == 2));

    integer tmpx = llSubStringIndex(value, mask);
    if(~tmpx) {
        integer diff = llStringLength(value) - llStringLength(mask);
        return  ((!tmpy && !diff)
             || ((tmpy == 1) && (tmpx == diff))
             || ((tmpy == 2) && !tmpx)
             ||  (tmpy == 3));
    }
    return FALSE;
}

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.

Deep Notes

Source

Posted here with the kind permission of Very Keynes. Originally posted February 2008 at http://forums-archive.secondlife.com/54/c9/243445/1.html .

Signature