?
Solved

-- == null ??????

Posted on 1998-09-15
4
Medium Priority
?
148 Views
Last Modified: 2010-03-05
I have this simple logic code for perl

if ( ($rev==undef) || ($rev=="new") ) {
    $rev = "new"; }
else {
    $rev = "rev" . $rev;
}

now this works fine when I pass in normal characters to $rev
but when I set $rev = "--" or "-"
it assumes $rev is undef and sets it to "new".
Is - a special character in perl?
why is this code not working properly and how to I get it to do what I want it to do?
0
Comment
Question by:ueihp
  • 2
4 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 1204871
perl -we '$rev = "--"; if( ($rev=="new") ){ $rev = "new"; }'
Argument "new" isn't numeric in eq at -e line 1.
Argument "--" isn't numeric in eq at -e line 1.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204872
ozo's right

You'd be better off with

if ( !defined($rev) ) {
   $rev = 'new';
} elsif ($rev ne 'new') {
   $rev  = 'rev' . $rev;
}



0
 
LVL 1

Accepted Solution

by:
stormerider earned 0 total points
ID: 1204873
Or, closer to the original code,

if ( ($rev == undef) || ($rev eq "new") ) {
   $rev = "new";
} else {
   $rev = "rev" . $rev;
}


0
 
LVL 85

Expert Comment

by:ozo
ID: 1204874
But since == will evaluate undef to be ==0, anything numerically ==0 will be ==undef
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

850 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