Ron Smith am Sonntag, 19. Februar 2006 18.47:
> Hi all,

Hi Ron

>   This page accepts a series of numbers, separated by spaces, and gives the
> values listed bellow.
>   I'm getting the following error, 

No information about the input that causes the error; are there also inputs 
not causing an 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.

Hm, line 14 contains the die "Please..."

>
> 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*)$/ ) {

This does not match inputs containing more than one number.

What you want is something like

/^\s*((?:\d+\s*?)+)\s*$/

The inner (?:) does not capture output (see perldoc perlre), 
and the regex trims the input (which is allowed to contain leading and 
trailing space)

>  $userIn = $1;
> } else {
>  die "Please, enter numbers separated by spaces only!: $!";
> }

The if-else could be shortened to (untested, so please check):

die "Bla" unless ($userIn)=$userIn=~/^\s*((?:\d+\s*?)+)\s*$/;

> my @numbers = split( / /, $userIn );

This only splits on a single space character.

my @numbers = split /\s+/, $userIn;

splits (explicitly) on the same whitespace allowed in the regex above.



hth!
Hans

[irrelevant parts snipped away]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to