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: Intermediate:
Adding up a column in array

 



Warren Bell
Deleted

Dec 31, 2000, 9:30 AM

Post #1 of 4 (776 views)
Adding up a column in array Can't Post

I have an array that will look like this but with about 50 rows:

web 901 0.0 2.6 4388 1748 ?
web 968 0.0 2.6 4352 1704 ?
root 18932 0.0 0.6 1032 404 tty1
root 29881 0.0 1.2 1592 812 ttyS1
postfix 30258 0.0 1.0 1584 668 ?
root 30861 0.2 1.1 1424 760 ?
root 30862 0.4 1.6 1996 1096
wjbell 30868 1.5 2.1 2248 1372
wjbell 30888 0.0 1.0 2284 716

I need to add up all the numbers in the third column while keeping the decemal point. So if I have:

0.5
1.6

it will come out to be:

2.1

Does anyone know the easiest way of doing this?



japhy
Enthusiast

Dec 31, 2000, 10:29 AM

Post #2 of 4 (774 views)
Re: Adding up a column in array [In reply to] Can't Post

Just iterate over the array, and add the third field to the running total, and then display the output.


Code
my $sum = 0; 
for (@array) { $sum += (split)[2] }
printf "Total: %.1f\n", $sum;

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Warren Bell
Deleted

Dec 31, 2000, 11:23 AM

Post #3 of 4 (770 views)
Re: Adding up a column in array [In reply to] Can't Post

Cool, it works, but can you explain what it's doing? I've never used a couple things before and, in case I need them later I can use them.

# is it proper perl code to always declare a variable with my?
my $sum = 0;

# I guess $snum is going to add as it goes along the array but not sure about (split)[2]
for (@array) { $sum += (split)[2] }

# not sure what this whole line is doing
printf "Total: %.1f\n", $sum;

Thanks for the responce.



japhy
Enthusiast

Dec 31, 2000, 2:00 PM

Post #4 of 4 (768 views)
Re: Adding up a column in array [In reply to] Can't Post

You're asking questions about what functions are doing (split() and printf()). These can probably be answered by taking a look at the perlfunc documentation in one of the following ways:

1. typing perldoc -f split and perldoc -f printf at your command prompt
2. referring to the chapter in "Programming Perl" that lists the built-in functions and their uses (the same as the perlfunc docs, really)
3. reading the content online at http://www.perldoc.org/

If you're not familiar with perldoc, get familiar. It saves everyone a lot of time, and it will make you a smarter, better, and more learned Perl programmer.

I will, though, explain my code.


Code
my $sum = 0;

I create this variable with my() because I am assuming the program is being run under the use strict pragma (which you should be using). It makes a local variable (a variable that is not global), which disappears from existence after the block it is in closes (so long as nothing is relying on its existence, like a reference).


Code
for (@array) { $sum += (split)[2] }

This iterates over the values in an array, called @array. Each element can be accessed by the "pronoun" variable $_. I then call split(), which, given no arguments, assumes it's being called as split(' ', $_), which will return a list of non-whitespace chunks of text. I wrap the call in parentheses to treat the return value like a list, and then use the [2] subscript on it to get the third element in the list (the first element has an index of 0, so the third element has an index of 2).


Code
printf "sum = %.1f\n", $sum;

The printf() function (borrowed from C) takes a formatting string and a list of values to format per the string. The %f format means "insert a floating point number here", and the .1 information in it means "1 digit after the decimal point". This is needed to make the number 6 appear as 6.0. If you do not want to print the value, but rather store the formatted value, use the sprintf() function instead:


Code
$formatted_sum = sprintf "%.1f", $sum;

I hope this clears things up for you.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


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

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