Solved

0 vs '': detecting

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now