# $EPIC: splice.txt,v 1.3 2007/03/02 02:32:04 jnelson Exp $ ======Synopsis:====== $__splice__( []) ======Technical:====== * If the argument is omitted the empty string is returned. * If the argument is omitted the empty string is returned. * If the argument is omitted the empty string is returned. * If the argument is omitted it is taken as the empty string. * is taken as the name of an [[ASSIGN]] variable. * and are taken as integers. * If is negative, and the absolute value of is greater than the number of words in $ the empty string is returned. * If is negative, then the number of words in $ is added to it, to simulate words from the end. * It is not easy to explain how this function works using bullet points. There are four values created using $ and , and three of the four are put together to create a new $ and the fourth value is the return value. * Let be the value of $; \\ Let be the number of words in $; \\ Let all word indexes count from zero: * If is greater than \\ Part 1 -- Words 0 through of (eg, all of ) \\ Part 2 -- The empty string \\ Part 3 -- The empty string \\ * If is greater than \\ Part 1 -- Words 0 through ( - 1) from \\ Part 2 -- Words through from orig \\ Part 3 -- The empty string * If is less than \\ Part 1 -- Words 0 through ( - 1) \\ Part 2 -- Words through ( + - 1) from \\ Part 3 -- Words ( + ) through from \\ * Let Part 4 be * The new value of $ is created as: \\ @ = \\ push \\ push * The return value of the function is * Remember that is a list of [[what is a word|words]]. ======Practical:====== Whew. The above technical description is a mouthful and is very confusing. In practical terms, what this function does is "splice" a variable by removing some words from the variable and pasting in other words in their place. It will remove words starting at word (counting from zero, of course), and put in their place. You can chop off the end of a string by using an unreasonably large value of . You can start counting from the end of the string by using a negative . The return value of the function is the text that was removed from the variable. When you get down to the bottom line, you can use this function to change one or more words in a function without having to rewrite the whole thing. If you have a word index returned by the matching functions, you could use it to search-and-replace words in a variable. ======Returns:====== The through +th (counting from 0) words from the variable $. As a side effect, the through +th words in $ are replaced with . If any error occurs, $ is UNCHANGED. ======Examples:====== @ foo = [one two three four five] $splice(foo 1 3 foo bar blah) returns "two three four" $splice(foo 0 2) returns "one foo" $splice(foo 2 1) returns "five" echo $foo shows "bar blah", end result