?
Solved

0 vs '': detecting

Posted on 2003-11-17
3
Medium Priority
?
243 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 260 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 240 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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
Suggested Courses

764 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