elegant way to replace numbers

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

elegant way to replace numbers

Post by jmburnod » Sat Mar 14, 2015 11:20 am

Hi All,
I need replace all num of a list
I use this:

Code: Select all

function getTextWithoutNumber pText
   put "0123456789" into tNum
   repeat for each char tReplace in tNum
      replace tReplace with "" in pText
   end repeat
   return pText
end getTextWithoutNumber
but I guess there is a more elegant way.
Best regards
Jean-Marc
https://alternatic.ch

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: elegant way to replace numbers

Post by Thierry » Sat Mar 14, 2015 11:51 am

jmburnod wrote:Hi All,
I need replace all num of a list
...
but I guess there is a more elegant way.
Bonjour Jean-Marc,

more elegant, Umm, kind of subjective feeling..

Anyway, here is one *I* feel strongly elegant :)

Code: Select all

function getTextWithoutNumber pText
   return replaceText( pText, "\d+", empty)
end getTextWithoutNumber
HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: elegant way to replace numbers

Post by dunbarx » Sat Mar 14, 2015 5:04 pm

Thierry does this in one line, as usual.

But I think that Jean-Marc, by formulating his handler so that only ten iterations are required to filter any length of text, as opposed to looping through each char in the text itself:

Code: Select all

...
repeat for each char tChar in tText
if tChar is not in "0123456789" then put tChar after tFilteredText...
is already pretty elegant. I only mention this to catch the eye of relatively new users who might appreciate this method, as opposed to that even more "basic" way...

Craig

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: elegant way to replace numbers

Post by Thierry » Sat Mar 14, 2015 7:09 pm

dunbarx wrote:Thierry does this in one line, as usual.
Well, I'm using a pure Livecode build-in function :roll:

Code: Select all

...
repeat for each char tChar in tText
if tChar is not in "0123456789" then put tChar after tFilteredText...
is already pretty elegant.
Certainly,
and it's always nice and instructive to show different solutions
and let the reader decide...

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: elegant way to replace numbers

Post by dunbarx » Sat Mar 14, 2015 8:34 pm

Thierry.
Well, I'm using a pure Livecode build-in function
:roll: ??

Of course you are. And I even use regex, though I need to tread carefully in that arena, where you are adept.

I just want to teach.

Craig

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: elegant way to replace numbers

Post by Thierry » Sun Mar 15, 2015 9:09 am

dunbarx wrote:I just want to teach.
Yes, and so do I; a mixture of helping and sharing.

Have a nice sunday,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: elegant way to replace numbers

Post by jmburnod » Mon Mar 16, 2015 1:15 pm

@Thierry,
Exactly what I have searched
Thanks

@Craig,
Yes LiveCode is a wonderful tool for learn coding
From simple to complex we can try different ways
Best regards
Jean-Marc
https://alternatic.ch

Post Reply