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

BASIC-like string functions

 [1/5] from: greggirwin::starband::net at: 27-Aug-2001 20:06


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? 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

 [2/5] from: joel:neely:fedex at: 27-Aug-2001 17:32


Hi, Gregg, 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
<<quoted lines omitted: 3>>
> (as in the second version of right), or is copy the right > way to go?
If you really want to "equate to BASIC's" string functions, then you will want to copy, as that's the real behavior of BASIC. If you use the non-copying version of Right, and subsequently modify the resulting string value, you'll find that you've also changed the original, due to sharing of the underlying sequence of characters. -jn- -- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

 [3/5] 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
<<quoted lines omitted: 8>>
> 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.
<<quoted lines omitted: 4>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.johnsons-web.com

 [4/5] from: greggirwin:starband at: 28-Aug-2001 10:24


Hi Joel,
>>
If you really want to "equate to BASIC's" string functions, then you will want to copy, as that's the real behavior of BASIC. If you use the non-copying version of Right, and subsequently modify the resulting string value, you'll find that you've also changed the original, due to sharing of the underlying sequence of characters.
>>
Right, thanks for confirming that for me. Even though I can see where the non-copy version might be nice from an efficiency standpoint, I think it's better to be safe and have them all conform to the same behavior. Thanks, --Gregg

 [5/5] from: greggirwin:starband at: 28-Aug-2001 10:29


Thanks Tim! Being new to REBOL, I'll take any examples I can get to see different ways that things can be done. Your example brings up another question since, with my background, ltrim and rtrim would trim whitepsace from the end of a string, which is obviously different than your view. I'll post a question about namespaces. --Gregg

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted