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

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

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

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

Postby 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
--
LiveCode_Panos
 
Posts: 225
Joined: Fri Feb 06, 2015 4:03 pm

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

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

Panos,

That works GREAT!

Thanks,

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

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

Postby 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
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 4696
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Postby 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

http://2108.co.uk/tinyide/ - TinyIDE gives you more space to develop your ideas

http://2108.co.uk/script-editor-themer/ Script Editor Themer - Your code has never looked so good

My software never has bugs. It just develops random features
AndyP
 
Posts: 276
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)

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

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

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

Postby 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
Thierry Douez - https://sunny-tdz.com
sunnY(rex - text2speech - perl - midi - image - pdf)
Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 514
Joined: Wed Nov 22, 2006 3:42 pm
Location: France

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

Postby 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 :)
Klaus
 
Posts: 9274
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany

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

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

Thanks Klaus and Thierry!
DR White
 
Posts: 370
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

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

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

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

Postby 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
Thierry Douez - https://sunny-tdz.com
sunnY(rex - text2speech - perl - midi - image - pdf)
Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 514
Joined: Wed Nov 22, 2006 3:42 pm
Location: France

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

Postby 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/3055742/regex-to-remove-all-non-numeric-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/regular_expressions/
Andy Piddock

http://2108.co.uk/tinyide/ - TinyIDE gives you more space to develop your ideas

http://2108.co.uk/script-editor-themer/ Script Editor Themer - Your code has never looked so good

My software never has bugs. It just develops random features
AndyP
 
Posts: 276
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)

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

Postby 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.
SparkOut
 
Posts: 1599
Joined: Sun Sep 23, 2007 4:58 pm

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

Postby 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 :)
Thierry Douez - https://sunny-tdz.com
sunnY(rex - text2speech - perl - midi - image - pdf)
Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 514
Joined: Wed Nov 22, 2006 3:42 pm
Location: France


Return to Getting Started with LiveCode - Complete Beginners

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron