Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

0 vs '': detecting

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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 …
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

618 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