Insert and delete Commas

Jan Schenkel janschenkel at yahoo.com
Wed Sep 18 16:57:00 EDT 2002


Hi Ken,

Congratulations on yet another excellent regular
expression solution. It's just not an automated
response in my brain set ;-)
And what-do-you-know : Ken's version even fits in the
Starter Kit 10-lines limit. Now where did I put that
perl-book again...

*drowsy smile*

Jan Schenkel.

"As we grow older, we grow both wiser and more foolish
at the same time."  (De Rochefoucald)

--- Ken Ray <kray at sonsothunder.com> wrote:
> Or, using Regular Expressions (my fave):
> 
> function insertCommas pNumber
>     local tMinusHold,tMainNum,tDecimalHold
>     get
>
matchText(pNumber,"([-]?)([0-9]*)[\.]?([0-9]*)",tMinusHold,tMainNum,tDecimal
> Hold)
>     if it is true then  -- should be, but can't hurt
> to check
>         put "" into returnVal
>         if tDecimalHold <> "" then put "." before
> tDecimalHold
>         repeat with x = length(tMainNum) down to 1
>             if ((length(tMainNum) - x) mod 3 = 0)
> and (x <>
> length(tMainNum)) then \
>               put "," before returnVal
>             put char x of tMainNum before returnVal
>         end repeat
>         return tMinusHold & tMainNum & tDecimalHold
>     end if
> end insertCommas
> 
> And to explain the regEx (which is something I do
> every time I make a regex
> post), here's how it breaks up:
> 
> ([-]?)
>     Look for 0 or 1 occurrence ("?") of a hyphen
> ("[-]"), and return it
> ("()") into the first variable provided
> (tMinusHold).
> 
> ([0-9]*)
>     Then look for 0 or more occurrences ("*") of a
> number ("[0-9]"), and
> return it ("()") into the second variable provided
> (tMainNum).
> 
> [\.]?
>     Then look for 0 or 1 occurence ("?") of a
> decimal point ("\." - the "\"
> escapes this 'special' character so it's not
> misinterpreted). Since there's
> no parentheses, don't return anything, just use it
> as a delimiter.
> 
> ([0-9]*)
>     Finally, look for 0 or more occurrences ("*") of
> a number ("[0-9]"), and
> return it ("()") into the last variable provided
> (tDecimalHold).
> 
> Hope this helps,
> 
> Ken Ray
> Sons of Thunder Software
> Email: kray at sonsothunder.com
> Web Site: http://www.sonsothunder.com/
> 
> [snip]

__________________________________________________
Do you Yahoo!?
Yahoo! News - Today's headlines
http://news.yahoo.com



More information about the use-livecode mailing list