I want to match a chunk exactly with replace

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

I want to match a chunk exactly with replace

Post by Da_Elf » Sat Aug 19, 2017 3:11 pm

I thought wholematches would work but it doesnt. I only want to remove "1" which shows up twice

Code: Select all

on mouseUp
   put "22,34,12,1,121,1" into list
   set the wholematches to true
   replace "1" with "" in list
   replace ",," with "," in list
   if the last char of list is "," then delete the last char of list
   answer list
end mouseUp

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: I want to match a chunk exactly with replace

Post by [-hh] » Sat Aug 19, 2017 3:26 pm

You could try this.

Code: Select all

on mouseUp
  filter items of "22,34,12,1,121,1" without "1" into lst
  answer lst
end mouseUp
shiftLock happens

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: I want to match a chunk exactly with replace

Post by Klaus » Sat Aug 19, 2017 3:29 pm

Hi Da_Elf,

I might repeat myself, but the dictionary is your friend! :D
About "Wholematches":
...
Specifies whether the lineOffset, wordOffset, and itemOffset functions search only for entire lines, words, or items.
...
No mention of "replace". 8)

Do this:

Code: Select all

on mouseUp
   put "22,34,12,1,121,1" into tList
   set the wholematches to true
   put empty into tNewList
   repeat for each item tItem in tList
      if itemoffset(tItem,tNewList) = 0 then
         put tItem & "," after tNewList
      end if
   end repeat
   delete  char -1 of tNewList
   answer tNewList
end mouseUp
In LC it is often faster to create a new list than modify an existing list!


Best

Klaus

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: I want to match a chunk exactly with replace

Post by Da_Elf » Sat Aug 19, 2017 6:31 pm

both great answers. thanks guys.

Post Reply