Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: BASIC-like string functions

From: tim:johnsons-web at: 27-Aug-2001 20:47

Here are code examples for my rtrim and ltrim functions. Following the code is an illustrative interpreter session. As you will see, the original data is not changed UNLESS an original word is assigned from a function: ;CODE rtrim: func[arg[series!] n[integer!]][ return arg: copy/part arg ((length? arg) - n) ] ltrim: func[arg[series!] n[integer!]][return skip arg n] ; session:
>> t: "onetwo"
== "onetwo"
>> u: rtrim t 2
== "onet"
>> t
== "onetwo"
>> t: rtrim t 2
== "onet"
>> t
== "onet"
>> t: "onetwo"
== "onetwo"
>> u: ltrim t 2
== "etwo"
>> t
== "onetwo" ; I don't know if I'm contributing anything here, but something to ; compare your approach to... On Mon, Aug 27, 2001 at 08:06:12PM -0600, Gregg Irwin wrote:
> Ramping up on REBOL after being a BASIC/VB guy, I didn't see any words that > equate to BASIC's Left, Right, and Mid which return their respective part > (left, right, or middle) of a given string. So here's what I came up with: > > left: func [s len][copy/part s len] > right: func [s len][copy at s ((length? s) - (len - 1))] > mid: func [s start len][copy/part at s start len] > > right can also be written without using copy: > > right: func [s len][at s ((length? s) - (len - 1))] > > Given that these functions seem to work, and not just on strings of course, > I'd like to hear from some real REBOLs if I'm missing anything obvious. For > example, is there a way to do it without using copy (as in the second > version of right), or is copy the right way to go?
You said something very KEY here, and that is that rebol's hierarchical data typing allows rtrim and ltrim to work on any variable-length data, and weeds out things like integer, which would produce error messages or (WORSE) unpredictable results.
> If I missed native words, or if this is just a horribly non-REBOL idiom, I'd > really like to hear that as well. > > Thanks! > > --Gregg > > -- > To unsubscribe from this list, please send an email to > [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.johnsons-web.com