Solved

0 vs '': detecting

Posted on 2003-11-17
3
222 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
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

770 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