Convert 1 (540) 444-2045 to 15404442045? - Solved

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
DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by DR White » Mon Mar 20, 2017 3:35 pm

Is the an easy way to Convert 1 (540) 444-2045 to 15404442045?

If there isn't, no big deal.

I can use a several Offset functions to clean it up, but thought there might be an easier way.

Thanks,

David
Last edited by DR White on Mon Mar 20, 2017 4:12 pm, edited 1 time in total.

LiveCode_Panos
Livecode Staff Member
Livecode Staff Member
Posts: 818
Joined: Fri Feb 06, 2015 4:03 pm

Re: Convert 1 (540) 444-2045 to 15404442045?

Post by LiveCode_Panos » Mon Mar 20, 2017 3:56 pm

Hi DR White,

What about this?

Code: Select all

on mouseUp
   answer myFun("1 (540) 444-2045")
end mouseUp

function myFun pIn
   local tOut
   repeat for each char tChar in pIn
      if isNumber(tChar) then
         put tChar after tOut
      end if
   end repeat
   return tOut
end myFun
Best,
Panos
--

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Convert 1 (540) 444-2045 to 15404442045?

Post by DR White » Mon Mar 20, 2017 4:12 pm

Panos,

That works GREAT!

Thanks,

David

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by dunbarx » Tue Mar 21, 2017 3:35 am

Just for a different way to think about stuff:

Code: Select all

replace space with empty in yourString
replace "(" with empty in yourString
replace ")" with empty in yourString
replace "-" with empty in yourString
Craig Newman

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by AndyP » Tue Mar 21, 2017 8:52 am

In one line :D

Code: Select all

 put replacetext("1 (540) 444-2045","[^0-9]","") 
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by DR White » Thu Mar 23, 2017 1:36 pm

Craig and Andy,

Thanks for the other solutions for converting mixed text to numbers.

Andy,

Where in the world to you find the option [^0-9] for replacetext?

When I use:

put "1 (540) 444-2045" into Test
put replacetext(Test,"[^0-9]","")

The string Test has not been converted>

Thanks,

David

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

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by Thierry » Thu Mar 23, 2017 2:11 pm

DR White wrote: When I use:

put "1 (540) 444-2045" into Test
put replacetext(Test,"[^0-9]","")

The string Test has not been converted>
Hi David,

the converted text is the output of the function replacetext()

Therefore, try this:

Code: Select all

put "1 (540) 444-2045" into Test
put replacetext(Test,"[^0-9]","") into Test
put Test
HTH,

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

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by Klaus » Thu Mar 23, 2017 2:16 pm

Hi David,
DR White wrote:When I use:

Code: Select all

    put "1 (540) 444-2045" into Test
   put replacetext(Test,"[^0-9]","")
The string Test has not been converted
"replacetext" is a function and does not modify its parameters!
But its OUTPUT is what you need! :D

Code: Select all

...
put "1 (540) 444-2045" into Test
put replacetext(Test,"[^0-9]","") into TEST2
answer TEST2
## = converted string!
...
Best

Klaus

P.S.
Ah, Thierry beat my by a couple of minutes :)

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by DR White » Thu Mar 23, 2017 2:56 pm

Thanks Klaus and Thierry!

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by DR White » Thu Mar 23, 2017 8:20 pm

Thierry,

Where did you find the info to use "[^0-9]"? in the replacementtext function?

Thanks,

David

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

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by Thierry » Fri Mar 24, 2017 7:58 am

DR White wrote:Thierry,

Where did you find the info to use "[^0-9]"? in the replacementtext function?
David,

This one is a copied/pasted of Andy's post :)

More seriously must be some information in the dictionary,
plus a RegexBuilder plugin
IDE Menu -> Development->Plugins->RegexBuilder
and I remember about a very simple stack with more details about regex
in sample stacks delivered with the IDE: still there?

The net is full of information about regex
but be careful there are a lot of different flavors of regex,
depending mainly of the regex engine and the language embedding it.

Kind regards,

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

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by AndyP » Fri Mar 24, 2017 9:56 am

Initially I did a Google search with "regex dictionary exclude non numbers" which came up with lots of hits using this "[^0-9]".

eg. http://stackoverflow.com/questions/3055 ... -or-period

Then I found some good explanation sources for Regex here

http://www.visca.com/regexdict/tutorial.html

http://www.pnotepad.org/docs/search/reg ... pressions/
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by SparkOut » Fri Mar 24, 2017 9:58 am

Thierry fell into the regex barrel when he was a baby, and had to eat his way through the whole lot to save himself from suffocation.

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

Re: Convert 1 (540) 444-2045 to 15404442045? - Solved

Post by Thierry » Fri Mar 24, 2017 10:24 am

SparkOut wrote:Thierry fell into the regex barrel when he was a baby,
and had to eat his way through the whole lot to save himself from suffocation.
the truth is, at this time, I had one foot in a regex barrel
and the other one in the Hypercard barrel!


Enjoy your day :)
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”