Category Archives: string manipulation

Graphics Class Methods in XML

Actionscript:
  1. // for simplicity I left this XML inline, this will work exactly the same if it were external
  2. var program:XML=<body>
  3.                 <draw>
  4.                   <![CDATA[
  5.                    beginFill(0xFF0000);
  6.                    drawCircle(100,100,50);
  7.                    endFill();
  8.                    lineStyle(0, 0x666666);
  9.                    moveTo(100, 100);
  10.                    lineTo(200, 200);
  11.                    moveTo(300, 200);
  12.                    curveTo(350, 300, 400, 200);
  13.                    lineStyle(0, 0x0000FF);
  14.                    drawRect(200, 50,100,100) ;
  15.                     ]]>
  16.                 </draw>
  17. </body>;
  18.  
  19. // parse and run the Graphics class commands from the XML
  20. render(parseFunctions(program.draw.toString()));
  21.  
  22. function parseFunctions(dat:String):Array{
  23.     var a:Array = dat.split(";") ;
  24.     for (var i:int = 0; i<a.length-1; i++){
  25.         a[i] = a[i].split(/\(\)|\(|\)/g);
  26.         var f:String = a[i][0] = a[i][0].replace(/\s/g,"");
  27.         a[i] = a[i].splice(0, a[i].length - 1);
  28.         if (a[i].length> 1){
  29.          a[i] = a[i][1].split(",");
  30.          a[i].unshift(f);
  31.         }
  32.     }
  33.     return a.splice(0,a.length - 1);
  34. }
  35. function render(p:Array):void {
  36.     for (var i:int = 0; i<p.length; i++) {
  37.         graphics[p[i][0]].apply(graphics,p[i].splice(1));
  38.     }
  39. }

The above code builds on yesterdays post by showing how one could potentially store graphics class method calls in XML using a few regular expressions and Function.apply().

The parseFunctions() function reads through the CDATA string and formats it in a 2D array that looks like this:

Actionscript:
  1. [[beginFill, 0xFF0000], [drawCircle, 100, 100, 50], etc...]

The render() function reads through this 2D array, using the first value of each nested array as the function and the remaining values as arguments...

As is this won't really work with most of the new fp10 graphics methods...

Also posted in Graphics, XML, dynamic, external data, functions, strings | Tagged , | Leave a comment

Adding Very Large Numbers

Actionscript:
  1. // tracing out large numbers will go into exponent notation
  2. trace(1000000000000000000000000);
  3. // outputs: 1e+24
  4.  
  5. var a:String =  "1000000000000000000000009";
  6. var b:String = "12000000000000000000000001";
  7.  
  8. // bigAdd function adds two large numbers
  9. trace(bigAdd(a, b));
  10. // outputs: 13000000000000000000000010
  11.  
  12. // unclear what regular addition is doing here:
  13. trace(int(a) + int(b));
  14. // outputs: -3758096384
  15.  
  16. function bigAdd(a:String, b:String):String{
  17.        var answer:String="";
  18.        var carry:int = 0;
  19.        var zeros:int = 0;
  20.        var i:int = 0;
  21.        var max:int = 0;
  22.        
  23.         // obtain max variable and prepend zeros to the shortest string
  24.        if (a.length> b.length){
  25.           max = a.length;
  26.           zeros = max - b.length;
  27.            for (i= 0; i<zeros; i++) {
  28.                 b = "0" + b;
  29.            }
  30.        }else if (b.length>  a.length){
  31.            max = b.length;
  32.            zeros = max - a.length;
  33.            for (i= 0; i<zeros; i++) {
  34.                 a = "0" + a  ;
  35.             }
  36.        }else{
  37.            // a and b have same number of digets
  38.            max = a.length;  
  39.        }
  40.        
  41.        // add each character starting with the last (max - 1),
  42.        // carry the 1 if the sum has a length of 2
  43.        for (i = max - 1;  i> -1; i--){
  44.                var sum:String = String(int(a.charAt(i)) + int(b.charAt(i)) + carry);
  45.  
  46.                if (sum.length == 2){
  47.                        answer = sum.charAt(1) + answer;
  48.                        carry = 1;
  49.                }else{
  50.                        carry = 0;
  51.                        answer = sum + answer;
  52.                }
  53.        }
  54.        if (carry == 1){
  55.            answer = 1 + answer;
  56.        }
  57.        return answer;
  58. }

This snippet demos a function called bigAdd(), which can be used to add very large numbers that the standard numerical datatypes (int, Number, etc) won't really work with. It uses a string based addition algorithm.

When I decided to write this snippet I dug around for awhile trying to find a nice elegant algorithm to port, something done without prepending zeros etc.... but I couldn't find one that suited my needs... so I just created this one from scratch. There's room for optimization and improvement but it works well enough for just playing around.

BigDecimal & BigInteger

A few months back I needed to do some programming with very large numbers. I dug around for awhile and eventually found out that java has a built in BigInteger andBigDecimal class.... I used java to write the program I had in mind, but thought it would be fun to eventually create something similar for ActionScript....

Also posted in Number, misc, strings | Tagged , , , , | 2 Comments

Format Phone #

Actionscript:
  1. var phoneField:TextField = new TextField();
  2. with (phoneField) {
  3.     type=TextFieldType.INPUT;
  4.     maxChars=12;
  5.     restrict="0-9";
  6.     border=true;
  7.     width=100;
  8.     height=20;
  9.     x=y=20;
  10. }
  11. addChild(phoneField);
  12.  
  13. phoneField.addEventListener(TextEvent.TEXT_INPUT, onInput);
  14.  
  15. function onInput(evt:TextEvent):void {
  16.     if (phoneField.length==3 || phoneField.length==7) {
  17.         phoneField.appendText("-");
  18.         var leng:int=phoneField.text.length;
  19.         phoneField.setSelection(leng, leng);
  20.     }
  21. }

Quick way to make sure text input is formatted like a phone number.

Posted in string manipulation | Tagged , , | Leave a comment

String.replace()

Actionscript:
  1. var words:String = "ActionSnippet.com is a website. ActionSnippet.com is a blog.";
  2.  
  3. // outputs: "ActionSnippet.com is a website. ActionSnippet.com is a blog.";
  4. trace(words);
  5.  
  6. // the "/g" tells it to replace all instances of ActionSnippet.com
  7. words = words.replace(/ActionSnippet.com/g, "SomeWebsite.com");
  8.  
  9. // outputs: SomeWebsite.com is a website. SomeWebsite.com is a blog.
  10. trace(words);

Just a quick response to a student question.

Also posted in strings | Tagged , , | Leave a comment