|
-
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??
-
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);
-
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
-
Thank You Moot, but now it only shows 16 for the output no matter what is pasted into the text box...
-
FK'n_dog
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
-
Make myString equal to whatever var is assigned to the textbox.
myString = myTextBoxVar;
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|