473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 27480
numberwhun
3,509 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

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

Similar topics

3
by: Paul | last post by:
The following code: <?php header("Content-Type: text/plain"); $numbers = array(3.714, 0.7142857142857143, 9.667, 6.333, 7, 6, 8, 2, 3, -2, 0, -6.6, 2.25, 4.333, -8, 3, 3.141592654, 5.5, 6.5,...
10
by: Ole | last post by:
Goodday everybody, i want to create an array that represents data, that has to be transferred to a pdf doc ( iTextSharp ). The problem is, that i seem too loose my faith. Namely, ( i have...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
12
by: Steve | last post by:
here's a quirk i can't seem to handle, just hack. since call-time by-reference is depreciated and i don't want to enable it in the php.ini, i'm kind of stuck when i want to pass userdata as an...
1
by: Nightfarer | last post by:
Hello. I have a big trouble using System.Array class (it's the first time I use it) for a software I'm developing. I have a form with a textbox(numbers) and one button (done). Once the number...
2
by: David TG | last post by:
Hiya! I would like to take a partial slice of a fairly complex array. The source looks something like array ( ==array ( ==NN
2
by: tuananh87vn | last post by:
Hi ! can anyone help me with the following topic: Find All Duplicates in a List of Numbers - Array implementation - -InitializeTree() -AddNode() -Add into...
8
by: jabernet | last post by:
Is it possible in the new DB2 9.5 to use the content of an array in an IN expression? Say we have the ddl: CREATE TYPE Numbers AS Integer ARRAY; CREATE tab1 AS ( id integer, numbers...
1
by: niner | last post by:
I have been working on the following code for about 6 hours now. I am attempting to write a program in Java that finds the highest, lowest, sum, and mean of an array. The program should also end...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.