Solved

0 vs '': detecting

Posted on 2003-11-17
3
240 Views
Last Modified: 2010-03-04
Have a program where I output a tab separated file into simple flat XML:

while (<STDIN>) {
  chomp;
  $line = $_;
  @Elems = split(/\t/, $line);

  print "<entry name=\"", $Elems[0], "\" id=\"", $Elems[1], "\"";
  if ($Elems[2] != '') {
      print " default=\"", $Elems[2], "\""
  }
  print "/>\n";

My problem is the line   if ($Elems[2] != '') {
Sometimes the default is 0 and when this is so the the next line is not executed (the same as if there was no value at all).

For example on the following sample:
abc      12
def      13      0
ghi      14      1
fjk      15      

the output is:
<entry name="abc" id="12"/>              
<entry name="def" id="13"/>              
<entry name="ghi" id="14" default="1"/>
<entry name="fjk" id="15"/>

How do I get around this?
0
Comment
Question by:davelane
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
BioI earned 65 total points
ID: 9764914
I think the problem is that you use != for a numeric value.
When you change "!=" into "ne", I think it will work.
When you use in the beginning of your script the addition of "-w" in the first line:
#!/usr/bin/per -w
you should have got an error message like:
Argument "" isn't numeric in numeric ne (!=) at ./yourscript.pl line xx.


while (<STDIN>) {
  chomp;
  $line = $_;
  @Elems = split(/\t/, $line);

  print "<entry name=\"", $Elems[0], "\" id=\"", $Elems[1], "\"";
  if ($Elems[2] ne '') {
     print " default=\"", $Elems[2], "\""
  }
  print "/>\n";
0
 
LVL 20

Assisted Solution

by:jmcg
jmcg earned 60 total points
ID: 9764990
I thought this was answered in the Perl FAQ, but I didn't find it in the place where I thought it should be.

Perl has both numeric comparison operators and string comparison operators. You were using a numeric comparison.


if ($Elems[2] != '') { # numeric comparison


And the empty string evaluates numerically to 0 (as do all other strings that do not look sufficiently like numbers). What you may have wanted to do was use a string comparison:


if ($Elems[2] ne '') { # string comparison


Let me just add one more complication. Variables in perl can also have a "value" called 'undef', or undefined. You can't use comparison operators to detect it, it has a special predicate function 'defined'. When used in comparisons, it acts the same as an empty string.

0
 

Author Comment

by:davelane
ID: 9765135
Thanks guys for the speedy replies. Did the trick. :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question