469,366 Members | 2,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Sum of Numbers in an Array

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. @array = (1..1000);
  4.  
  5. sub total {
  6. my($v1) = print @array;
  7.  
  8. my($output) = &total();
  9.  
I'll figure it out. Don't need your help. Thanks. Since the array is just

@array = (1..1000);
print @array;

or

some bs, this thing sucks.
Sep 29 '11 #1

✓ answered by numberwhun

Just because I was in a coding mood:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my @array = (1..10);
  7. my $sum = 0;
  8.  
  9. foreach my $num (@array){
  10.     $sum = $sum + $num;
  11. }
  12.  
  13. print("The sum is:  $sum\n");
  14.  
Please notice the pragmas at the beginning, "use strict;" and "use warnings;". You want to use those at the beginning of every Perl script you write. They will save you a ton of headache.

Regards,

Jeff

5 26402
numberwhun
3,503 Expert Mod 2GB
Just because I was in a coding mood:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my @array = (1..10);
  7. my $sum = 0;
  8.  
  9. foreach my $num (@array){
  10.     $sum = $sum + $num;
  11. }
  12.  
  13. print("The sum is:  $sum\n");
  14.  
Please notice the pragmas at the beginning, "use strict;" and "use warnings;". You want to use those at the beginning of every Perl script you write. They will save you a ton of headache.

Regards,

Jeff
Sep 30 '11 #2
That helps a bunch. Man I still got a lot to learn. That little trick
$1 = $1 + $other didn't know you could do that. Yah man thanks again since I actually got so pissed, I broke something. At least I don't own a gun.
Sep 30 '11 #3
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my @array = (1..1000);
  7. my $sum = 0;
  8.  
  9. foreach my $num (@array){
  10.     $sum = $sum + $num;
  11. }
  12.  
  13. sub total {
  14. my($v1) = print $sum;
  15. }
  16.  
  17.  
  18. my($output) = &total();
  19. print "$output\n";
  20.  
Happy as a clown. Only thing is, I'm right when I say the foreach is not a subroutine, it's a loop. Yah so this is correct.
Sep 30 '11 #4
chorny
80 Expert
Expand|Select|Wrap|Line Numbers
  1. use List::Util 'sum';
  2. $sum=sum(@array);
  3.  
Sep 30 '11 #5
Thank You, I will try one too. So kind of you both to help me. Really appreciate that.
Sep 30 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by inkexit | last post: by
24 posts views Thread by Michael | last post: by
12 posts views Thread by Steve | last post: by
2 posts views Thread by David TG | last post: by
8 posts views Thread by jabernet | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.