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

replace

 [1/7] from: ryan::christiansen::intellisol::com at: 26-Jun-2001 12:44


Please help me to understand 'replace. Why doesn't the following return an error?
>> a: "foobar"
== "foobar"
>> c: "fabulous"
== "fabulous"
>> replace a c c
== "foobar"
>>
-Ryan

 [2/7] from: jelinem1:nationwide at: 26-Jun-2001 14:15


Are you are referring to the fact that the string you specified to replace, "fabulous", is not in the main string "foobar"? Or that you are trying to replace a string with itself? In the first case, no search/replace tool I've used has complained (esp given an error) that zero replaces were made. The second case I can't see as an error - except maybe a programmer error. Certainly not a language error, or one that would interrupt the 'replace. It is simply a trivial case of 'replace. - Michael Jelinek From: [ryan--christiansen--intellisol--com]@rebol.com on 06/26/2001 12:44 PM Please respond to [rebol-list--rebol--com] Sent by: [rebol-bounce--rebol--com] To: [rebol-list--rebol--com] cc: Subject: [REBOL] replace Please help me to understand 'replace. Why doesn't the following return an error?
>> a: "foobar"
== "foobar"
>> c: "fabulous"
== "fabulous"
>> replace a c c
== "foobar"
>>
-Ryan

 [3/7] from: ryan:christiansen:intellisol at: 26-Jun-2001 14:21


I was trying to use 'try on a block containing a 'replace statement and couldn't understand why it would not return an error when trying to replace a string that doesn't exist. I guess 'replace simply doesn't error out. Why not? I think it should. -Ryan [JELINEM1--nati] onwide.com To: [rebol-list--rebol--com] Sent by: cc: rebol-bounce@ Subject: [REBOL] Re: replace rebol.com 06/26/2001 01:15 PM Please respond to rebol-list Are you are referring to the fact that the string you specified to replace, "fabulous", is not in the main string "foobar"? Or that you are trying to replace a string with itself? In the first case, no search/replace tool I've used has complained (esp given an error) that zero replaces were made. - Michael Jelinek Please help me to understand 'replace. Why doesn't the following return an error?
>> a: "foobar"
== "foobar"
>> c: "fabulous"
== "fabulous"
>> replace a c c
== "foobar"
>>
-Ryan

 [4/7] from: arolls:bigpond:au at: 27-Jun-2001 5:56


Nah... Just do this: a: "foobar" c: "fabulous" either find a c [replace a c c][ ;it wasn't found ] or even (if a is long) either t: find a c [replace t c c][] Just think, you would have to couch each 'replace with an either error? try [][][] structure. How annoying. Anton.

 [5/7] from: ryan:christiansen:intellisol at: 26-Jun-2001 15:32


But... shouldn't replace return an error, anyway, if you are asking it to replace something that doesn't exist? "Anton" <[arolls--bigpo] To: <[rebol-list--rebol--com]> nd.net.au> cc: Sent by: Subject: [REBOL] Re: replace rebol-bounce@ rebol.com 06/26/2001 02:56 PM Please respond to rebol-list Nah... Just do this: a: "foobar" c: "fabulous" either find a c [replace a c c][ ;it wasn't found ] or even (if a is long) either t: find a c [replace t c c][] Just think, you would have to couch each 'replace with an either error? try [][][] structure. How annoying. Anton.

 [6/7] from: joel:neely:fedex at: 26-Jun-2001 16:22


Hi, Ryan, Let me offer my reasons why I think there should not be an error: 1) If you care whether the substring occurs, you can test that condition yourself prior to attempting the REPLACE. 2) Many times one uses REPLACE (and especially REPLACE/ALL) in situations where the occurrence is optional. In such cases, one *wants* a function that does nothing gracefully. For example, I recently worked on a document management app that took user-supplied "topic" phrases and replaced spaces with underscores to create file names. Two examples of this replacement would be:
>> replace/all "my new topic" " " "_"
== "my_new_topic"
>> replace/all "testing" " " "_"
== "testing" (Of course, the first argument was supplied in a variable, but you get the idea.) The point is that the "eliminate whitespace" operation automatically succeeds in the trivial case when there is no whitespace to be eliminated. 3) The burden of additional coding should be on the exceptional case where one cares (1), rather than on the more common case where one does not (2), at least IMHO. -jn- [ryan--christiansen--intellisol--com] wrote:
> I was trying to use 'try on a block containing a 'replace > statement and couldn't understand why it would not return an error > when trying to replace a string that doesn't exist. I guess > 'replace simply doesn't error out. Why not? I think it should. >
...
> Please help me to understand 'replace. Why doesn't the following > return an error?
<<quoted lines omitted: 5>>
> == "foobar" > >>
-- It's turtles all the way down! joel'dot'neely'at'fedex'dot'com

 [7/7] from: jelinem1:nationwide at: 26-Jun-2001 17:16


Let's forget that no replace function - in any language that I've ever used - will ever generate an error upon "zero replacements", and build one that makes the most sense. If REBOL 'replace were to generate an error under this condition, then - as Anton pointed out - you would have to wrap every ARBITRARY call to 'replace with a 'try structure or use an "if find" statement to ensure that the search string exists in the parent string. IMO this goes against the principle of making simple tasks simple. If you really want to trap the case where the search string does not exist in the parent string, this really echoes the functionality of 'find (again, Anton gave an example of this, below). If you really want to change the behaviour of 'replace, feel free to redefine it. Do a "source replace". Copy the code and modify it. "Have it YOUR way!" :) - Michael Jelinek From: [ryan--christiansen--intellisol--com]@rebol.com on 06/26/2001 03:32 PM Please respond to [rebol-list--rebol--com] Sent by: [rebol-bounce--rebol--com] To: [rebol-list--rebol--com] cc: Subject: [REBOL] Re: replace But... shouldn't replace return an error, anyway, if you are asking it to replace something that doesn't exist? "Anton" <[arolls--bigpo] To: <[rebol-list--rebol--com]> nd.net.au> cc: Sent by: Subject: [REBOL] Re: replace rebol-bounce@ rebol.com 06/26/2001 02:56 PM Please respond to rebol-list Nah... Just do this: a: "foobar" c: "fabulous" either find a c [replace a c c][ ;it wasn't found ] or even (if a is long) either t: find a c [replace t c c][] Just think, you would have to couch each 'replace with an either error? try [][][] structure. How annoying. Anton.

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