CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
adding array elements

 



airo
stranger

Apr 9, 2001, 7:11 PM

Post #1 of 3 (584 views)
adding array elements Can't Post

here's the deal, I have an array with a varying number of elements (all numbers 0.5, 1, 1.2, 2.4, .....)
How can I iterate through the array adding each element to achieve a single variable as the total?
Remember the array may not ever have the same number of elements.
thanks
paul




Jean
User


Apr 10, 2001, 12:59 AM

Post #2 of 3 (580 views)
Re: adding array elements [In reply to] Can't Post

You can use the foreach in order to loop through an array with dynamically changing number of elements.
Another option is use of $#array expression which returns the last index in the @array.

Example:

Code
foreach $i (@array) { 
$sum += $i;
}

or

for ($i = 0; $i <= $#array; $i++) {
$sum += $array[$i];
}

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


airo
stranger

Apr 10, 2001, 8:31 AM

Post #3 of 3 (576 views)
Re: adding array elements [In reply to] Can't Post

thanks it worked great
paul


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives