Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

null values

Status
Not open for further replies.

Cagliostro

Programmer
Sep 13, 2000
4,226
GB
Hi, I have a problem
Code:
use strict;
my $x;

if($x){print("x\n");}else{print("!x\n");}
if($x == 0){print("x == 0\n");}else{print("x != 0\n");}
if($x == "0"){print("x == \"0\"\n");}else{print("x != \"0\"\n");}
if($x == "00"){print("x == \"00\"\n");}else{print("x != \"00\"\n");}
if($x == ""){print("x == \"\"\n");}else{print("x != \"\"\n");}
if($x == " "){print("x == \" \"\n");}else{print("x != \" \"\n");}
if($x == "  "){print("x == \"  \"\n");}else{print("x != \"  \"\n");}

my %xx;

if($xx{'abc'}){print("xx{'abc'}\n");}else{print("!xx{'abc'}\n");}
if($xx{'abc'} == 0){print("xx{'abc'} == 0\n");}else{print("xx{'abc'} != 0\n");}
if($xx{'abc'} == "0"){print("xx{'abc'} == \"0\"\n");}else{print("xx{'abc'} != \"0\"\n");}
if($xx{'abc'} == "00"){print("xx{'abc'} == \"00\"\n");}else{print("xx{'abc'} != \"00\"\n");}
if($xx{'abc'} == ""){print("xx{'abc'} == \"\"\n");}else{print("xx{'abc'} != \"\"\n");}
if($xx{'abc'} == " "){print("xx{'abc'} == \" \"\n");}else{print("xx{'abc'} != \" \"\n");}
if($xx{'abc'} == "  "){print("xx{'abc'} == \"  \"\n");}else{print("xx{'abc'} != \"  \"\n");}

printf("x = {$x}\n");
$x = 0;
printf("x = {$x}\n");
$x = "00";
printf("x = {$x}\n");
$x = " ";
printf("x = {$x}\n");
$x = "  ";
printf("x = {$x}\n");
this is the output:
Code:
!x
x == 0
x == "0"
x == "00"
x == ""
x == " "
x == "  "
!xx{'abc'}
xx{'abc'} == 0
xx{'abc'} == "0"
xx{'abc'} == "00"
xx{'abc'} == ""
xx{'abc'} == " "
xx{'abc'} == "  "
x = {}
x = {0}
x = {00}
x = { }
x = {  }

The value of variables comes from a CGI application, so, I have to know if there was passed some string, some number, some string with spaces or some string with zeroes. How can I test this thing? In each situation what is different than another one, the test is the same, but behavior is different...

Ion Filipski
1c.bmp
 
You should have warnings turned on, which would have helped to pinpoint your problem. Everything enclosed in quotes is a string, and you're using numeric comparison (==), whereas you should be using 'eq' for all the string comparisons.
 
I think this might help - although I can't understand the necessity of checking for $variable == 0 as this gives false positives:-

Code:
[b]#!/usr/bin/perl[/b]

$x = '"0"';
checkThisOut("x", "$x");

$xx{'abc'} = '" "';
checkThisOut("xx{'abc'}", "$xx{'abc'}");

sub checkThisOut {

  $variableName = shift;
  $toCheck = shift;
  
  if ($toCheck) {
    print "$variableName\n";
  } else {
    print "!$variableName\n";
  }
  
  if ($toCheck == 0) {
    print "$variableName == 0\n";
  } else {
    print("$variableName != 0\n");
  }
  
  @toCheckAgainst = ('"0"', '"00"', '""', '" "', '"  "');
  
  foreach $item (@toCheckAgainst) {
    if ($item eq $toCheck) {
      print "$variableName eq $item\n";
    } else {
      print "$variableName ne $item\n";
    }
  }
  
}

outputs:-

[red][tt]x
x == 0
x eq "0"
x ne "00"
x ne ""
x ne " "
x ne " "
xx{'abc'}
xx{'abc'} == 0
xx{'abc'} ne "0"
xx{'abc'} ne "00"
xx{'abc'} ne ""
xx{'abc'} eq " "
xx{'abc'} ne " "[/tt][/red]


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top