Page 1 of 1

Replace

Posted: Thu Jul 28, 2016 6:59 pm
by Da_Elf
I dont get this. Replace isnt replacing chunks properly
Here i have setup a list with
Mike Watkins
Mike
Mark

I want to replace just Mike on line 2.
theList is as above.
theNewName is "Mel"
theOldText is "Mike"

ALL instances of mike get changed

Code: Select all

      set the wholeMatches to true
      put the lineoffset(theOldText,theList) into changeLine
      replace line changeLine of theList with theNewName in theList

Re: Replace

Posted: Thu Jul 28, 2016 8:25 pm
by mwieder
try

Code: Select all

replace theOldText with theNewName in line changeLine of theList

Re: Replace

Posted: Thu Jul 28, 2016 8:32 pm
by SparkOut

Code: Select all

   --Mark's method above WORKS - for an individual line.
   --I'm not absolutely sure how you want the final list to be rewritten but this should help track down what you need to do,
   --if you need to replace multiple lines which may or may not be the case.

   put "Mike Watkins" & cr & "Mike" & cr & "Mike Baker" & cr & "Mike" & cr & "Mike Peabody" & cr & "Mark" into theList
   put "Mel" into theNewName
   put "Mike" into theOldText
   set the wholeMatches to true

   --no "THE" with a function, it's not a property
   put lineoffset(theOldText,theList) into changeLine
   put line changeLine of theList into tChangeMe

   --the bit here:
   --[line changeLine of theList]
   --resolves to "Mike" in this case, so
   --your line:
   --replace line changeLine of theList with theNewName in theList

   --is equivalent to saying:
   --replace "Mike" with "Mel" in theList

   --so try:
   replace cr & line changeLine of theList & cr with cr & theNewName & cr in theList
   --as a quick and simple way to restrict the change to the whole line without resorting to regex

Re: Replace

Posted: Thu Jul 28, 2016 8:46 pm
by Da_Elf
There is only supposed to be one line changed so mark's works perfectly

Re: Replace

Posted: Thu Jul 28, 2016 9:45 pm
by mwieder
Da_Elf-

I was responding to a *very specific* use case here. SparkOut is right in that you shouldn't have "the" before the function call... I noticed that and forgot to comment on it.

For a more general use, you've already found how to replace *all* occurrences. You'd need a loop if you want to replace more than one and less than all.

Re: Replace

Posted: Fri Jul 29, 2016 10:10 am
by hpsh
maybe i don't really get the problem, i am not any good in english, but what about:

Code: Select all

   put "Mel" into line 2 in field "list"