Page 1 of 1

elegant way to replace numbers

Posted: Sat Mar 14, 2015 11:20 am
by jmburnod
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

Re: elegant way to replace numbers

Posted: Sat Mar 14, 2015 11:51 am
by Thierry
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

Re: elegant way to replace numbers

Posted: Sat Mar 14, 2015 5:04 pm
by dunbarx
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

Re: elegant way to replace numbers

Posted: Sat Mar 14, 2015 7:09 pm
by Thierry
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

Re: elegant way to replace numbers

Posted: Sat Mar 14, 2015 8:34 pm
by dunbarx
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

Re: elegant way to replace numbers

Posted: Sun Mar 15, 2015 9:09 am
by Thierry
dunbarx wrote:I just want to teach.
Yes, and so do I; a mixture of helping and sharing.

Have a nice sunday,

Thierry

Re: elegant way to replace numbers

Posted: Mon Mar 16, 2015 1:15 pm
by jmburnod
@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