Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding numbers in and array

Status
Not open for further replies.

newtarge

ISP
Oct 27, 2003
21
US
Hi,

I have a problem trying to add numbers in an array there is 1 number per line of the array. I want to add all line togather.

help!!
 
If I understand your explanation correctly, the following should work.
Code:
$total = 0;
foreach $line (@array) {
    if ($line =~ /(\d+)/) {
        $total += $1;
    }
}
 
Thank you very much Raider2001 it worked well, except if the number had a decimal in it "10.4" would ignore the .4.
 
Changing the regular expression should fix that problem. Try the following:
Code:
$total = 0;
foreach $line (@array) {
    if ($line =~ /(\d+\.*\d*)/) {
        $total += $1;
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top