Q: Text replacement function
|
|
Teddy Wishbringer
Snuggly Bear Cub
Join date: 28 Nov 2004
Posts: 208
|
05-01-2006 09:10
Hiyas all. I recently ran into an application where I need to replace sections of a string with a variable (or other text) on the fly. Let me give you a example. Say I wanted to use a chat command like: /greet Teddy I would like it to come out in a say as: Hi there Teddy, how are you today? I would use something like string greeting = "Hi there {name}, how are you today?" Is there a function that I can say like function("{name}","Teddy"  ? I ran through the Wiki looking at the string functions and couldn't see any string replacement functions.
|
|
Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
|
05-01-2006 09:19
First thought that comes to mind for this particular instance . . . . use llGetSubstring to get the portion of the string after the known command "/greet" Assign that to a variable and insert it in what you want to say . . . . string name = llGetSubString(command_string, 7, -1); //want to start with the characters after the command. string greeting = "Hi there "+name+", how are you today?";
|
|
Teddy Wishbringer
Snuggly Bear Cub
Join date: 28 Nov 2004
Posts: 208
|
05-01-2006 09:39
That's a great suggestion and would definately work for certain situations. I appreciate the suggestion.
For my application, I find it necessary to have both predefined strings in the script and the ability to read replacement ones in from a notecard, thus I pretty much need to find a way to insert new phrases in after the string has already been defined.
I do appricate your response though. I did an extensive search of the scripting forums and haven't found really anything in the way of string replacement options, and I'm sure your suggestion will be of assistance to others doing a future search as it may be suitable for their needs.
Thanks again.
|
|
Thraxis Epsilon
Registered User
Join date: 31 Aug 2005
Posts: 211
|
05-01-2006 10:08
There is a str_replace function on the wiki as an example for the llInsertString function. http://secondlife.com/badgeo/wakka.php?wakka=llInsertString
|
|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
05-01-2006 10:21
You could split up your pre-defined string into a prefix and a suffix. That way your final string becomes prefix + insertedName + suffix. You could set the notecard up accordingly, so you read in the 2 parts as separate entries. That's a suggestion, not sure if it'll work very smoothly in your application.
|
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
05-01-2006 10:37
str_replace is a good a function (i wrote it), and not a bad way to do dynamics.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river. - Cyril Connolly
Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence. - James Nachtwey
|
|
Adriana Caligari
Registered User
Join date: 21 Apr 2005
Posts: 458
|
05-01-2006 11:15
// // define your triggers // list triggers = ["{name}","{surname}","{address_1}","{adress_2}" ]; // // get data for your replacements - notecard etc // list replacements = ["Fred","Bloggs","town","Country" ] ; // you populate this from your own data // . . . listen( integer ch, string nm, key id, string mess ) { list temp = llParseString2List( mess,[" "],[] );// split the text into words // integer idx ; for ( idx = 0; idx < llGetListLength( temp ); idx++ ) { string tmpa = llList2String( temp,idx ); // pull out a word // integer idxb = llListFindList( triggers,tmpa ) ;// is it one of the {} words ? // if ( idxb >= 0 ) { string replace = llList2String( replacements,idxb ); // get the replacement string // temp = llListReplaceList( temp,[replace],idx,idx ) ; // replace trigger with data // } } // string output = llDumpList2String( temp," " ) ; // rebuild the ouput string // } }
That should work - havent tested it tho - so excuse any syntax's
|
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
05-01-2006 11:26
Using Adriana's method on strings with alot of spaces can result in a Stack Heap Collision, if you have a complex script. Recently I was using a similar method for removing leading spaces from a string (Parsed the string to a list using a space as the seperator, then took the first element of the list, did a substringindex for the first element on the list in the original string, and deleting everything before that index; method was sound, unfortunatly memory requirements were too much).
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river. - Cyril Connolly
Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence. - James Nachtwey
|
|
Teddy Wishbringer
Snuggly Bear Cub
Join date: 28 Nov 2004
Posts: 208
|
05-01-2006 11:44
Awesome suggestions! I'll give the str_replace a shot first and see how that works out. Adriana's suggestion looks good too, but with my application of it requires the use of dynamic variables being placed in the string instead of fixed ones from a list.. some of which need to be crosschecked first to ensure it has valid data (ie: a name or whatnot) before being inserted. Thank you very much for all the recommendations, it's muchly appreciated!! On a side note, passing a list along with dynamic replacements would be handy especially in the case of the same string requiring multiple replacements (ie: "Greetings {name}, the time is currently {time} on {date}"  in one pass instead of multiples. But I should be able to safely adapt the str_replace function to fullfill those needs... I hope. *laughs*
|
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
05-01-2006 12:14
The list method is an N^2 run time where N is the number of list entries. The string replacement method is a n*m, where n is the number of tags in the string, m is the length of the string. I think string replacement will be faster, but it really depends. The string method should use less memory, but I cannot be sure.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river. - Cyril Connolly
Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence. - James Nachtwey
|