Difference between revisions of "Talk:LlSubStringIndex"
(14 intermediate revisions by 7 users not shown) | |||
Line 19: | Line 19: | ||
In what way is [[llSubStringIndex]] not RegEx? -- '''[[User:EddyFragment Robonaught|Eddy]]''' <sup><small>([[User talk:EddyFragment_Robonaught|talk]]|[[Special:Contributions/EddyFragment_Robonaught|contribs]])</small></sup> 06:15, 10 August 2009 (UTC) | In what way is [[llSubStringIndex]] not RegEx? -- '''[[User:EddyFragment Robonaught|Eddy]]''' <sup><small>([[User talk:EddyFragment_Robonaught|talk]]|[[Special:Contributions/EddyFragment_Robonaught|contribs]])</small></sup> 06:15, 10 August 2009 (UTC) | ||
:It only looks for complete matches of the search string. You don't have the opportunity to pass wildcards for certain words/characters or something like that. Like the first appearence of "I <WILDECARD> you" in a given string. RegEx are way more powerful than what [[llSubStringIndex]] provides. --[[File:Zai_signature.png|45px|link=User:Zai Lynch]] <sup><small>([[User talk:Zai Lynch|talk]]|[[Special:Contributions/Zai Lynch|contribs]])</small></sup> 10:45, 10 August 2009 (UTC) | |||
Right? So you can search for what is in a string at a place defined by the kind of things you would expect around it rather than where in a string a CERTAIN thing is (or is not). That would save a lot of messing about. Thanx for the explanation Zai. I would have thanked you sooner but the wiki is playing up for me. Pages are not (resolving?) booting up. -- '''[[User:EddyFragment Robonaught|Eddy]]''' <sup><small>([[User talk:EddyFragment_Robonaught|talk]]|[[Special:Contributions/EddyFragment_Robonaught|contribs]])</small></sup> 11:19, 10 August 2009 (UTC) | |||
: But with [http://en.wikipedia.org/wiki/Regular_expression RegEx] it's a bit more complicated. With [[llSubStringIndex]] you can check the existence of a given search phrase. This is similar with the PHP function [http://de.php.net/manual/en/function.stripos.php strpos]. | |||
:'''Basic Example from the function page:''' | |||
:<lsl> | |||
integer index = llSubStringIndex("string data","TEST"); | |||
if(index == -1) { | |||
llSay(0,"TEST was not found in the string"); | |||
} else { | |||
llSay(0,"TEST was found in the string."); | |||
} | |||
</lsl> | |||
With regular expressions, i think, it's similar to the basic usage of [http://de.php.net/manual/en/function.preg-match.php preg_match], where you can search for real patterns. <br> | |||
'''senseless example:'''<br> | |||
We have a script that asks for your name and we know user-names can only have Latin alphabetical chars and numbers. | |||
<php> | |||
# desc: preg_match(string $pattern, string $subject) | |||
# Searches subject for a match to the regular expression given in pattern. | |||
# [A-Za-z0-9] say it can have all chars from A to Z and a to z besides numbers from 0 to 9 | |||
# the + behind is a special char (metacharacter) and say a character from [A-Za-z0-9] must occur once or more times. | |||
# the \s stands for a whitespace | |||
if(preg_match("~[A-Za-z0-9]+\s[A-Za-z0-9]+~", "Kuraiko Yoshikawa")); # TRUE | |||
if(preg_match("~[A-Za-z0-9]+\s[A-Za-z0-9]+~", "Kuraiko Yo$hikawa?")); # FALSE | |||
</php> | |||
In PHP, pregmatch can also return an array with the matches, so you have a ''"list"'' with all matched phrases. I guess this isn't realistic for LSL.<br> | |||
When LSL gets RegEx, we also need functions like [http://de.php.net/manual/en/function.preg-match.php preg_replace] and [http://de.php.net/manual/en/function.preg-split.php preg_split] on our wishlist ;).<br> | |||
[[Image:Kurai-paw-ico.jpg]] [[User:Kuraiko Yoshikawa|Kuraiko Yoshikawa]] 13:26, 10 August 2009 (UTC) | |||
<nowiki>*nods*</nowiki> oooh! pretty colors -- '''[[User:EddyFragment Robonaught|Eddy]]''' <sup><small>([[User talk:EddyFragment_Robonaught|talk]]|[[Special:Contributions/EddyFragment_Robonaught|contribs]])</small></sup> 13:43, 10 August 2009 (UTC) | |||
== Lindens == | == Lindens == | ||
Line 25: | Line 61: | ||
According to Soft Linden [https://wiki.secondlife.com/wiki/User_talk:Soft_Linden#sensor_caveat you can sense Lindens regardless of administrative status ("God Mode" or Not) if they are in an adjacent region and in sensor range]. -- '''[[User:EddyFragment Robonaught|Eddy]]''' <sup><small>([[User talk:EddyFragment_Robonaught|talk]]|[[Special:Contributions/EddyFragment_Robonaught|contribs]])</small></sup> 06:23, 10 August 2009 (UTC) | According to Soft Linden [https://wiki.secondlife.com/wiki/User_talk:Soft_Linden#sensor_caveat you can sense Lindens regardless of administrative status ("God Mode" or Not) if they are in an adjacent region and in sensor range]. -- '''[[User:EddyFragment Robonaught|Eddy]]''' <sup><small>([[User talk:EddyFragment_Robonaught|talk]]|[[Special:Contributions/EddyFragment_Robonaught|contribs]])</small></sup> 06:23, 10 August 2009 (UTC) | ||
== I | :I didn't clean up that caveat because I didn't want to draw more attention to the intentional loophole. -- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 02:28, 11 August 2009 (UTC) | ||
You want to remove the last 3 entries (Including this one)? -- '''[[User:EddyFragment Robonaught|Eddy]]''' <sup><small>([[User talk:EddyFragment_Robonaught|talk]]|[[Special:Contributions/EddyFragment_Robonaught|contribs]])</small></sup> 05:36, 11 August 2009 (UTC) | |||
:: no point, very few people ever read the talk pages except editors, and I'm going to assume most of us were already aware of it via Soft's answering of the question and leaving it there. <br/>-- '''[[User:Void_Singer|Void]]''' <sup><small>([[User_talk:Void_Singer|talk]]|[[Special:Contributions/Void_Singer|contribs]])</small></sup> 15:21, 11 August 2009 (UTC) | |||
Hi Void. I agree. Not something that can really be hidden (just like Lindens! lolz). And it is a shame that such an amazing resource (this wiki) isn't more fully plugged in world. I only found it by wondering what "Help" I would get by clicking Help>Scripting Portal... Considering the wealth of knowledge and expertise found betwixt these walls (hypothetical walls) I'd've thought it best to advertise its existence far more widely. <nowiki>*shrugs*</nowiki> -- '''[[User:EddyFragment Robonaught|Eddy]]''' <sup><small>([[User talk:EddyFragment_Robonaught|talk]]|[[Special:Contributions/EddyFragment_Robonaught|contribs]])</small></sup> 16:30, 11 August 2009 (UTC) | |||
== "Fixed" bug in starts/endswith implementation == | |||
When haystack == needle these tools failed: the llDeleteSubString would return an empty string. I've "fixed" the problem by tacking on a blank space, but I'm pretty sure there are more optimal ways to do so - string concat can be expensive. Got any ideas? [[User:Cron Stardust|Cron Stardust]] 20:21, 10 March 2013 (PDT) | |||
:"startswith" is easily handled by: !llSubStringIndex(haystack, needle) and "endswith" can be achieved via: llGetSubString (haystack, -llStringLength(needle), -1) == needle). In practice one would not code a UDF for "startswith" and probably not for "endswith" either. And certainly not for "contains", they should be coded in-line. I don't subscribe to the argument that null needles should be handled. In the very rare event that I needed to handle such, I would add an initial test for the null needle. There would probably wider reaching consequences that needed to be tackled. I see that the original snippet for "startswith" was similar to mine, but discarded for speed reasons. But as script size is often the more significant constraint, I would stick with the simple llSubStringIndex(). [[User:Omei Qunhua|Omei Qunhua]] 03:55, 11 March 2013 (PDT) | |||
:*sigh* Fix one bug, introduce another. The implementation on the page was originally based on llGetSubString, the problem with llGetSubString, it's incapable of returning an empty string (unless you push both the start and end past the end of the array). As such it fail to work on needles that are empty strings. I've put in some better constants that should keep it from deleting a character when they match. -- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 11:59, 11 March 2013 (PDT) |
Latest revision as of 11:00, 11 March 2013
startswith, endswith
Me or someone should return to contribute correct-at-a-glance versions of the snippets that mean:
startswith(whole, part) -- returns true if whole starts with part, else returns false
endswith(whole, part) -- returns true if whole ends with part, else returns false
cf. http://docs.python.org/lib/string-methods.html
-- Ppaatt Lynagh 15 October 2007 (PDT)
Soooo cool. Someone else did jump in to contribute! Now naturally I look forward to me or someone implementing startswith like we've implemented endswith, and then comparing the space & time costs of the two startswith implementation choices. -- Ppaatt Lynagh 16:21, 16 October 2007 (PDT)
RegEx
Any chance of RegEx in the future?
Cron Stardust 23:32, 7 March 2007 (PST)
- I would say, highly unlikely. It would break existing functionality. Best to request an entirely new function. Strife Onizuka 04:39, 8 March 2007 (PST)
In what way is llSubStringIndex not RegEx? -- Eddy (talk|contribs) 06:15, 10 August 2009 (UTC)
- It only looks for complete matches of the search string. You don't have the opportunity to pass wildcards for certain words/characters or something like that. Like the first appearence of "I <WILDECARD> you" in a given string. RegEx are way more powerful than what llSubStringIndex provides. -- (talk|contribs) 10:45, 10 August 2009 (UTC)
Right? So you can search for what is in a string at a place defined by the kind of things you would expect around it rather than where in a string a CERTAIN thing is (or is not). That would save a lot of messing about. Thanx for the explanation Zai. I would have thanked you sooner but the wiki is playing up for me. Pages are not (resolving?) booting up. -- Eddy (talk|contribs) 11:19, 10 August 2009 (UTC)
- But with RegEx it's a bit more complicated. With llSubStringIndex you can check the existence of a given search phrase. This is similar with the PHP function strpos.
- Basic Example from the function page:
- <lsl>
integer index = llSubStringIndex("string data","TEST"); if(index == -1) {
llSay(0,"TEST was not found in the string");
} else {
llSay(0,"TEST was found in the string.");
}
</lsl>
With regular expressions, i think, it's similar to the basic usage of preg_match, where you can search for real patterns.
senseless example:
We have a script that asks for your name and we know user-names can only have Latin alphabetical chars and numbers.
<php>
- desc: preg_match(string $pattern, string $subject)
- Searches subject for a match to the regular expression given in pattern.
- [A-Za-z0-9] say it can have all chars from A to Z and a to z besides numbers from 0 to 9
- the + behind is a special char (metacharacter) and say a character from [A-Za-z0-9] must occur once or more times.
- the \s stands for a whitespace
if(preg_match("~[A-Za-z0-9]+\s[A-Za-z0-9]+~", "Kuraiko Yoshikawa")); # TRUE if(preg_match("~[A-Za-z0-9]+\s[A-Za-z0-9]+~", "Kuraiko Yo$hikawa?")); # FALSE </php>
In PHP, pregmatch can also return an array with the matches, so you have a "list" with all matched phrases. I guess this isn't realistic for LSL.
When LSL gets RegEx, we also need functions like preg_replace and preg_split on our wishlist ;).
Kuraiko Yoshikawa 13:26, 10 August 2009 (UTC)
*nods* oooh! pretty colors -- Eddy (talk|contribs) 13:43, 10 August 2009 (UTC)
Lindens
As far as I know, sensor()s can't find Lindens. I had a Linden confirm it when I was trying to set him up as a guest driver on one of my boats. Maybe the example should be changed so it's not misleading? I'm gonna go make sure the sensor() page mentions that. Stickman 18:23, 14 August 2008 (PDT)
According to Soft Linden you can sense Lindens regardless of administrative status ("God Mode" or Not) if they are in an adjacent region and in sensor range. -- Eddy (talk|contribs) 06:23, 10 August 2009 (UTC)
- I didn't clean up that caveat because I didn't want to draw more attention to the intentional loophole. -- Strife (talk|contribs) 02:28, 11 August 2009 (UTC)
You want to remove the last 3 entries (Including this one)? -- Eddy (talk|contribs) 05:36, 11 August 2009 (UTC)
Hi Void. I agree. Not something that can really be hidden (just like Lindens! lolz). And it is a shame that such an amazing resource (this wiki) isn't more fully plugged in world. I only found it by wondering what "Help" I would get by clicking Help>Scripting Portal... Considering the wealth of knowledge and expertise found betwixt these walls (hypothetical walls) I'd've thought it best to advertise its existence far more widely. *shrugs* -- Eddy (talk|contribs) 16:30, 11 August 2009 (UTC)
"Fixed" bug in starts/endswith implementation
When haystack == needle these tools failed: the llDeleteSubString would return an empty string. I've "fixed" the problem by tacking on a blank space, but I'm pretty sure there are more optimal ways to do so - string concat can be expensive. Got any ideas? Cron Stardust 20:21, 10 March 2013 (PDT)
- "startswith" is easily handled by: !llSubStringIndex(haystack, needle) and "endswith" can be achieved via: llGetSubString (haystack, -llStringLength(needle), -1) == needle). In practice one would not code a UDF for "startswith" and probably not for "endswith" either. And certainly not for "contains", they should be coded in-line. I don't subscribe to the argument that null needles should be handled. In the very rare event that I needed to handle such, I would add an initial test for the null needle. There would probably wider reaching consequences that needed to be tackled. I see that the original snippet for "startswith" was similar to mine, but discarded for speed reasons. But as script size is often the more significant constraint, I would stick with the simple llSubStringIndex(). Omei Qunhua 03:55, 11 March 2013 (PDT)
- sigh* Fix one bug, introduce another. The implementation on the page was originally based on llGetSubString, the problem with llGetSubString, it's incapable of returning an empty string (unless you push both the start and end past the end of the array). As such it fail to work on needles that are empty strings. I've put in some better constants that should keep it from deleting a character when they match. -- Strife (talk|contribs) 11:59, 11 March 2013 (PDT)