A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Parse and add numbers in a string?

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    15

    Cool Parse and add numbers in a string?

    I'm wanting to make a small program that when people paste in some long text like below:

    05/01/2009 02:14:59 Ad Package Bonus in amount of 4.
    05/01/2009 02:14:59 Ad Package acquired in amount of 16.
    04/30/2009 11:05:37 Ad Package Bonus in amount of 2.
    04/30/2009 11:05:37 Ad Package acquired in amount of 11.


    That I can have a button that will ad up the numbers on the right side

    So the solution would be: 33

    Any Ideas??

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    This is how you can get everything in between "amount of " and "." from a string and turn that string into a number:

    Code:
    myString = "04/30/2009 11:05:37 Ad Package acquired in amount of 16."
    startNo = myString.indexOf("amount of ")+10;
    stopNo = myString.lastIndexOf(".");
    grabAmount = myString.substring(startNo,stopNo);
    trace("grabAmount="+grabAmount);

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    15

    Smile Doesn't add up

    No matter what is pasted in, it shows 16 only...
    See here
    http://www.thejoyluckclub.com/BasCalMX.swf


    The code behind the button:

    on (release) {


    myString = "04/30/2009 11:05:37 Ad Package acquired in amount of 16."
    startNo = myString.indexOf("amount of ")+10;
    stopNo = myString.lastIndexOf(".");
    grabAmount = myString.substring(startNo,stopNo);
    trace("grabAmount="+grabAmount);

    //answer text box below
    answer = grabAmount;


    }

    I'm wanting to make a small program that when people paste in some long text like below:

    05/01/2009 02:14:59 Ad Package Bonus in amount of 4.
    05/01/2009 02:14:59 Ad Package acquired in amount of 16.
    04/30/2009 11:05:37 Ad Package Bonus in amount of 2.
    04/30/2009 11:05:37 Ad Package acquired in amount of 11.


    That I can have a button that will ad up the numbers on the right side

    So the solution would be: 33

    Any Ideas??

    How can I get the code to look at every line of text??

    I know I'm close!
    Last edited by carpadeim12; 05-02-2009 at 01:39 PM. Reason: To improve it

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    15
    Thank You Moot, but now it only shows 16 for the output no matter what is pasted into the text box...

  5. #5
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    see if this method works for you -
    PHP Code:
    // input textfield - instance name - inp
    // button - instance name - btn

    function findNum(val) {
    for (var 
    i=0;i<=val.length;i++) {
    char val.charAt(i);
    if (!
    isNaN(char)){ break; }
    }
    return 
    parseInt(val.substr(i));
    };

    btn.onRelease = function(){
    total 0;
    arr = [];
    arr inp.text.split("\r");
    for(var 
    n=0;n!=arr.length;n++){
    arr[n] = arr[n].toString().substr(31); // remove date/time/Ad Package
    total += findNum(arr[n]);
    }
    inp.text "total = "+total;
    }; 
    copy/paste to inp textfield -
    05/01/2009 02:14:59 Ad Package Bonus in amount of 4.
    05/01/2009 02:14:59 Ad Package acquired in amount of 16.
    04/30/2009 11:05:37 Ad Package Bonus in amount of 2.
    04/30/2009 11:05:37 Ad Package acquired in amount of 11.

    results in total = 33

  6. #6
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Make myString equal to whatever var is assigned to the textbox.

    myString = myTextBoxVar;

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    15
    thanks so much that helps alot!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center