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:
my program gives many errors

 



digioleg54
User

Oct 5, 2016, 7:08 AM

Post #1 of 6 (3052 views)
my program gives many errors Can't Post


Code
#!/usr/bin/perl 
use strict;
use warnings;
while(1){
print "Please enter numbers, when you want to finish, enter quit\n";
my $num=<STDIN>;
chomp $num;
last if ($num eq 'quit');
my @number=();
push @number,$num;
}
print "All numbers are @number\n";
print "Amount: ", scalar(@number),"\n";

my ($sum, $min, $max) = (0, $number[0], 0);
foreach my $nm (@number) {
$sum += $nm;
$min = $nm if ($min > $nm);
$max = $nm if ($max < $nm);
}
print "Sum of all numbers: $sum\n";
print "Average: ", $sum / @number, "\n";
print "Smallest: $min; Largest: $max\n";

when I run it, errors:

Possible unintended interpolation of @number in string at ./arrnum.pl line 12.
Global symbol "@number" requires explicit package name at ./arrnum.pl line 12.
Global symbol "@number" requires explicit package name at ./arrnum.pl line 13.
Global symbol "@number" requires explicit package name at ./arrnum.pl line 15.
Global symbol "@number" requires explicit package name at ./arrnum.pl line 16.
Global symbol "@number" requires explicit package name at ./arrnum.pl line 22.
Execution of ./arrnum.pl aborted due to compilation errors.


(This post was edited by FishMonger on Oct 5, 2016, 7:35 AM)


FishMonger
Veteran / Moderator

Oct 5, 2016, 7:39 AM

Post #2 of 6 (3047 views)
Re: [digioleg54] my program gives many errors [In reply to] Can't Post

Please use the code tags whenever you post any code.

You have a scoping issue.

You declared @number inside the loop, which makes it private to the loop and is not available outside of the loop. If you need to use it after the loop, then you need to declare it prior to the loop.


digioleg54
User

Oct 5, 2016, 7:48 AM

Post #3 of 6 (3044 views)
Re: [FishMonger] my program gives many errors [In reply to] Can't Post

Thank you, I understand my error. It works perfect


Zhris
Enthusiast

Oct 5, 2016, 8:22 AM

Post #4 of 6 (3038 views)
Re: [digioleg54] my program gives many errors [In reply to] Can't Post

After fixing your scope issue, your code is fine as it is, I just wanted to introduce you to List::Util and here-docs.

List::Util has functions useful when operating on arrays, specifically the sum, min and max functions, which could replace your foreach loop.

Here-docs are useful when you want to print a multi-line string, you write output much like you would in a plain text editor, literal horizontal and vertical white spaces are honored without the need for escape sequences, also you have the ability to interpolate variables. In my opinion it increases clarity.


Code
use List::Util qw/sum min max/; 

...

my $sum = sum @numbers;
my $size = @numbers;
my $avg = $sum / $size;
my $min = min @numbers;
my $max = max @numbers;

print <<"EOL";
All numbers are: @numbers
Amount: $size
Sum of all numbers: $sum
Average: $avg
Smallest: $min
Largest: $max
EOL


Chris


(This post was edited by Zhris on Oct 5, 2016, 8:26 AM)


digioleg54
User

Oct 5, 2016, 8:28 AM

Post #5 of 6 (3033 views)
Re: [Zhris] my program gives many errors [In reply to] Can't Post

I know this, but I am working on iMac and there are some features not install


FishMonger
Veteran / Moderator

Oct 5, 2016, 8:39 AM

Post #6 of 6 (3031 views)
Re: [digioleg54] my program gives many errors [In reply to] Can't Post

List::Util has been a core module since perl v5.7.3. If you don't have it, then your perl installation is broken or you're using a really old perl.

You can install the module, if needed.

 
 


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

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