Difference between revisions of "Like"

From Second Life Wiki
Jump to: navigation, search
(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...)
 
m (<lsl> tag to <source>)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{LSL Header}} __NOTOC__
+
{{LSL_Function
<div id="box">
+
|mode=user
== Function: [[string]] like([[string]] {{LSL Param|arg1}}, [[string]] {{LSL Param|arg2}}); ==
+
|func=like
<div style="padding: 0.5em;">
+
|p1_type=string|p1_name=value
See if one word matches part of another.
+
|p2_type=string|p2_name=mask
 
+
|return_type=integer
To use it, you pass the function a value and a mask.
+
|return_text=that is [[TRUE]] if '''value''' tested positive for '''mask''', otherwise it is [[FALSE]].
 
+
|func_desc=See if one word matches part of another.
Examples:<br />
+
|func_footnote=
 
+
like("Susie", "Sus%") will return true, for any value starting with "Sus"<br />
+
like("Susie", "%Sus%") will return true, for any value containing "Sus"<br />
+
like("Susie", "%Sus") will return false. This example is looking for a string ending in "Sus".<br />
+
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.
 
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=
 +
<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 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.</source>
 +
|spec=<source lang="lsl2">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));
  
</div></div>
+
    integer tmpx = llSubStringIndex(value, mask);
<div id="box">
+
    if(~tmpx) {
== Specification ==
+
        integer diff = llStringLength(value) - llStringLength(mask);
<div style="padding: 0.5em;">
+
         return  ((!tmpy && !diff)
<lsl>
+
            || ((tmpy == 1) && (tmpx == diff))
integer like(string arg1, string arg2) {
+
            || ((tmpy == 2) && !tmpx)
        float tmpx; float tmpy;
+
            || (tmpy == 3));
        if(llGetSubString(arg2,0,0)=="%"){
+
    }
            arg2=llGetSubString(arg2,1,-1);tmpy=1;
+
    return FALSE;
        }
+
}</source>
        if(llGetSubString(arg2,llStringLength(arg2)-1,-1) == "%") {
+
|helpers
            arg2=llGetSubString(arg2,0,llStringLength(arg2)-2);tmpy=tmpy+2;
+
|also_functions
         }
+
|also_events
        if(-1!=(tmpx=llSubStringIndex(arg1,arg2))) {
+
|also_tests
            if((tmpy==0 && llStringLength(arg1)==llStringLength(arg2))
+
|also_articles
            ||(tmpy==1 && tmpx == (llStringLength(arg1)- llStringLength(arg2)))
+
|location=
            ||(tmpy==2 && tmpx == 0)
+
Posted here with the kind permission of {{User|Very Keynes}}. Originally posted February 2008 at http://forums-archive.secondlife.com/54/c9/243445/1.html .
            ||(tmpy==3)) {
+
|notes
                  return TRUE;
+
|cat1=Examples
            }
+
|cat2
        }
+
|cat3
        return FALSE;
+
|cat4
}
+
}}
 
+
 
+
</lsl>
+
</div></div>
+
 
+
Posted here with the kind permission of Very Keynes. Originally posted February 2008 at http://forums.secondlife.com/showthread.php?t=243445 .
+
 
+
{{LSLC|Examples|like}}
+

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