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 an array?

 



Stiffler
Novice

May 6, 2000, 8:14 PM

Post #1 of 3 (675 views)
adding an array? Can't Post

I have a data file which is sorted as such:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open (FILE, "<$userdata");
@PersonalPoints = <FILE>;
close (FILE);
foreach $pointline (@PersonalPoints) {
($date, $department, $realpoints) = split (/=/, $pointline);

</pre><HR></BLOCKQUOTE>

Now here is the tricky part. What I want to happen is for every listing that $department = "CLOTHES"; - I want it to take each $realpoints and ADD them all up?? So if the file had 2 entries where $department = "CLOTHES"; and for each of them $realpoints was 4 - the returned value would be 8.

Any ideas?


Jasmine
Administrator / Moderator

May 8, 2000, 4:05 PM

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

Not tricky at all. Consider the following:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


my $totalpoints;
foreach (@PersonalPoints) {
my ($date, $department, $realpoints) = split (/=/, $_);
if ($department eq "CLOTHES"){
$totalpoints += $realpoints;
}
print $totalpoints; # has total
</pre><HR></BLOCKQUOTE>

Hope this helps...


Stiffler
Novice

May 8, 2000, 4:54 PM

Post #3 of 3 (675 views)
Re: adding an array? [In reply to] Can't Post

That worked perfectly

thanks Smile

 
 


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

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