Ron Smith wrote:
> Hi all,
Hello,
> This page accepts a series of numbers, separated by spaces, and gives
> the values listed bellow.
>
> I'm getting the following error, but don't see why. Can anyone spot
> my error?
>
> Please, enter numbers separated by spaces only!: Bad file descriptor
> at E:/www/cgi-bin/pg93ex3.11SeriesOfNumbers.pl line 14.
>
> I've narrowed the problem down to the "if" block (the regex), I'd like
> to accept numbers and spaces only. Here's the code:
>
> #!E:/www/perl/bin/perl.exe
>
> use strict;
> use warnings;
> use CGI qw( :standard );
> use CGI::Carp qw( fatalsToBrowser );
> print header();
>
> my $userIn = param( "textfield" );
>
> if ( $userIn =~ /^(\d+|\s*)$/ ) {
> $userIn = $1;
> } else {
> die "Please, enter numbers separated by spaces only!: $!";
Your problem is that you are using the $! variable:
perldoc perlvar
[snip]
$! If used numerically, yields the current value of the C "errno"
variable, or in other words, if a system or library call fails,
it sets this variable. This means that the value of $! is
meaningful only immediately after a failure:
But you are using it for a pattern match failure which is neither a system nor
a library call.
> }
>
> my @numbers = split( / /, $userIn );
I would write that as:
my @numbers = $userIn =~ /\d+/g;
@numbers or die "Please, enter numbers separated by spaces only!";
> my $total = scalar( @numbers );
"my $total =" forces scalar context so using the scalar() function is redundant.
> my @sorted = sort( @numbers );
> my $smallestNum = @sorted[0];
> my $largestNum = @sorted[-1];
perldoc -q difference
Found in /usr/lib/perl5/5.8.6/pod/perlfaq4.pod
What is the difference between $array[1] and @array[1]?
The former is a scalar value; the latter an array slice, making it a
list with one (scalar) value. You should use $ when you want a scalar
value (most of the time) and @ when you want a list with one scalar
value in it (very, very rarely; nearly never, in fact).
Sometimes it doesn’t make a difference, but sometimes it does. For
example, compare:
$good[0] = ‘some program that outputs several lines‘;
with
@bad[0] = ‘same program that outputs several lines‘;
The "use warnings" pragma and the −w flag will warn you about these
matters.
Or you could write those three lines like this:
my ( $smallestNum, $largestNum ) = sort @numbers;
But since you are sorting numbers, you probably want to do a numeric sort to
get the correct numbers:
my ( $smallestNum, $largestNum ) = sort { $a <=> $b } @numbers;
And of course you can do that more efficiently using a for loop:
my ( $smallestNum, $largestNum ) = @numbers[ 0, 0 ];
for ( @numbers ) {
$smallestNum = $_ if $smallestNum > $_;
$largestNum = $_ if $largestNum < $_;
}
> my $sum = 0;
> foreach ( @numbers ) {
> $sum += $_;
> }
>
> my $average = ( $sum / $total );
You could just use the array there as a mathematical expression forces scalar
context:
my $average = ( $sum / @numbers );
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>