Insert and delete Commas

Ken Ray kray at sonsothunder.com
Wed Sep 18 12:43:01 EDT 2002


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/


----- Original Message -----
From: "Jan Schenkel" <janschenkel at yahoo.com>
To: <use-revolution at lists.runrev.com>
Sent: Wednesday, September 18, 2002 10:54 AM
Subject: Re: Insert and delete Commas


> Hi Bob,
>
> During a rare "awake" moment I also rescripted your
> other handler. This should handle very long numbers
> better than the SuperCard functions, as well...
>
> on insertCommaCd startNum, finishNum
> --syntax: insertCommaCd --[integer, integer]
>   repeat with x = startNum to finishNum
>     put insertCommas(fld x) into fld x
>   end repeat
> end insertCommaCd
>
> function insertCommas pNumber
>   -- first strip off and save the sign
>   if char 1 of pNumber is "-" then
>     put "-" into tMinusHold
>     delete char 1 of pNumber
>   else
>     put "" into tMinusHold
>   end if
>   -- now strip off and save the decimal part
>   get offset(".", pNumber)
>   if it > 0 then
>     put char it to -1 into tDecimalHold
>     delete char it to -1 of pNumber
>   else
>     put "" into tDecimalHold
>   end if
>   -- now determine how many commas to place
>   put length(pNumber) into tLength
>   put (tLength DIV 3) into tCommas
>   if (tLength MOD 3) = 0 then subtract 1 from tCommas
>   -- insert the commas into the integer part
>   repeat with i = tCommas down to 1
>     put "," after char - (i * 3 + 1) of pNumber
>   end repeat
>   -- put everything back together
>   return tMinusHold & pNumber & tDecimalHold
> end insertCommas
>
>
> Hope this helped,
>
> Jan Schenkel.
>
> "As we grow older, we grow both wiser and more foolish
> at the same time."  (De Rochefoucald)
>
> --- Robert Presender <rpresender at earthlink.net> wrote:
> > The following custom handlers(?) are used in an
> > application developed
> > in SuperCard.
> > They are used for math processes such as addition,
> > subtraction, etc..
> > They work in Rev.
> >
> > Would appreciate any comments relating to shorting
> > or modifying the
> > scripts or a different approach.  The scripts are to
> > be used on Mac and
> > Windows platforms.
> >
> > Regards ... Bob
> >
> > on insertCommaCd startNum, finishNum  --number must
> > end in .00.
> > --syntax: insertCommaCd --[integer, integer]
> >    repeat with x = startNum to finishNum
> >      if char 1 of fld x  is "-" then
> >        put "-" into minusHold
> >        delete char 1 of  fld x
> >      else
> >        put "" into minusHold
> >      end if
> >
> >      if "," is not in fld x then
> >        get the number of chars of  fld x
> >        if it >= 7 and it <= 9 then
> >          put "," after char (it -6) of  fld x
> >        else
> >          if it >= 10 then
> >            get the number of chars of  fld x
> >            put "," after char (it -9) of  fld x
> >            put "," after char (it -5) of  fld x
> >          end if
> >        end if
> >      end if
> >      put minusHold before char 1 of  fld x
> >    end repeat
> > end insertCommaCd
> >
> > --**Deletes commas from multiple or single  fields
> > having 1 or
> > --**more lines per  field. Minus numbers can be used
> > on deleteComma startNo, finishNo --syntax:
> > deleteComma [integer,
> > integer]
> >    repeat with x = startNo to finishNo
> >      get the number of lines of fld x
> >      repeat with y = 1 to it
> >        if "," is in line y of fld x then  --**
> >          get offset(",", line y of fld x )
> >          delete char it of line y of fld x
> >          if it is not 0 then
> >            get offset(",", line y of fld x )
> >            delete char it of line y of fld x
> >          else
> >            next repeat
> >          end if
> >        end if
> >      end repeat
> >    end repeat
> > end deleteComma
>
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! News - Today's headlines
> http://news.yahoo.com
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/use-revolution
>




More information about the use-livecode mailing list