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: Intermediate:
always strange message and myu if statement working incorrect

 



digioleg54
User

Jul 18, 2016, 9:00 AM

Post #1 of 7 (4254 views)
always strange message and myu if statement working incorrect Can't Post

I have a sub:

sub validate_queue{
my ($q) = @_;
print STDOUT "QUE = $q\n";
if ((undef($q) && $q < 1 && $q > 10) {

return 1;
}else{
return 0;
}
}

Every time I send there a number it says:

Use of uninitialized value $q in numeric lt (<) at G:\INVACCT\Perl\scripts\pfi_m
enu.pl line 1192, <STDIN> line 7.

And any number I send always return 1. Could you please help?


BillKSmith
Veteran

Jul 18, 2016, 10:11 AM

Post #2 of 7 (4249 views)
Re: [digioleg54] always strange message and myu if statement working incorrect [In reply to] Can't Post

Please post a working demonstration of your problem. Your function does not even compile. (Parenthesis do not balance in the if statement) I tried to 'fix' that by removing the extra left parenthesis. The resulting function returns zero (0) for every numeric input (including undef).
The print statement issues a warning message if the input is undef.


Code
use strict; 
use warnings;
foreach my $number ( 0 .. 11, undef ) {
my $status = validate_queue($number);
if (defined $number) {
printf "Argument: %2d Result: %d\n", $number, $status;
}
else {
printf "Argument: undef Result: %d\n", $status;
}

}
sub validate_queue{
my ($q) = @_;
print STDOUT "QUE = $q\n";
if (undef($q) && $q < 1 && $q > 10) {

return 1;
}else{
return 0;
}
}


OUTPUT:

Code
QUE = 0 
Argument: 0 Result: 0
QUE = 1
Argument: 1 Result: 0
QUE = 2
Argument: 2 Result: 0
QUE = 3
Argument: 3 Result: 0
QUE = 4
Argument: 4 Result: 0
QUE = 5
Argument: 5 Result: 0
QUE = 6
Argument: 6 Result: 0
QUE = 7
Argument: 7 Result: 0
QUE = 8
Argument: 8 Result: 0
QUE = 9
Argument: 9 Result: 0
QUE = 10
Argument: 10 Result: 0
QUE = 11
Argument: 11 Result: 0
Use of uninitialized value $q in concatenation (.) or string at C:\Users\Bill\pe
rl\guru\digioleg54.pl line 15.
QUE =
Argument: undef Result: 0

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jul 18, 2016, 10:15 AM

Post #3 of 7 (4247 views)
Re: [digioleg54] always strange message and myu if statement working incorrect [In reply to] Can't Post

Well, first, this condition

Code
$q < 1 && $q > 10

can never be true, since no number can be smaller than 1 AND larger than 10.

Then, this:


Code
undef($q)

does not do what you probably think.

If you want to test if $q is defined, then do it this way:


Code
if (defined $q ...


More than that, I can't really say, because you don't tell us what you want to do, and your code if too wrong for me to be able to figure out.

Just one possible shot in the dark:


Code
if (defined $q and ($q < 1 or $q > 10)) { 
return 1;
} else {
return 0;
}


But even if that is what you want, there may be a simpler way to write it.


digioleg54
User

Jul 18, 2016, 10:17 AM

Post #4 of 7 (4246 views)
Re: [BillKSmith] always strange message and myu if statement working incorrect [In reply to] Can't Post

Bill, I don't want to see that message, even queue is "". How can we do that?


Laurent_R
Veteran / Moderator

Jul 18, 2016, 10:33 AM

Post #5 of 7 (4244 views)
Re: [digioleg54] always strange message and myu if statement working incorrect [In reply to] Can't Post


In Reply To
Bill, I don't want to see that message, even queue is "". How can we do that?


What do you mean?


BillKSmith
Veteran

Jul 18, 2016, 11:42 AM

Post #6 of 7 (4239 views)
Re: [digioleg54] always strange message and myu if statement working incorrect [In reply to] Can't Post

The best way to be sure that your program does what you want under unusual conditions is to test for them and handle them explicitly.

Accepting Laurent's assumptions and this strategy, we have:

Code
use strict; 
use warnings;
use Scalar::Util qw(looks_like_number);
foreach my $number ( 0 .. 11, 'a', "", undef ) {
my $status = validate_queue($number);
if ( defined $number and looks_like_number($number) ) {
printf "Argument: %2d Result: %d\n", $number, $status;
}
else {
printf "Argument: undef or non_numeric Result: %d\n", $status;
}
}

sub validate_queue {
my ($q) = @_;
if ( !defined $q or !looks_like_number($q) ) {
print "QUE undefined or non-numeric\n";
return 0;
}
print "QUE = $q\n";
return ($q < 1 or $q > 10) ;
}


OUTPUT:

Code
QUE = 0 
Argument: 0 Result: 1
QUE = 1
Argument: 1 Result: 0
QUE = 2
Argument: 2 Result: 0
...
QUE = 9
Argument: 9 Result: 0
QUE = 10
Argument: 10 Result: 0
QUE = 11
Argument: 11 Result: 1
QUE undefined or non-numeric
Argument: undef or non_numeric Result: 0
QUE undefined or non-numeric
Argument: undef or non_numeric Result: 0
QUE undefined or non-numeric
Argument: undef or non_numeric Result: 0


Note use of module Scalar::Util. For alternate ways to test for a valid number, refer to FAQ.

Code
perldoc -q "How do I determine whether a scalar is a number"

Good Luck,
Bill


digioleg54
User

Jul 18, 2016, 12:01 PM

Post #7 of 7 (4237 views)
Re: [BillKSmith] always strange message and myu if statement working incorrect [In reply to] Can't Post

thank you, I already found the way. And I don't know if it works for windows

 
 


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

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