Go Premium for a chance to win a PS4. Enter to Win


PERL conditional equation error

Posted on 2008-10-06
Medium Priority
Last Modified: 2008-10-06
Hi -
I have a line of code in a PERL cgi script as follows -

 if($first_char == '\*')

When I run the script it generates the following error -

Argument "*" isn't numeric in numeric eq (==) at ............line 488

Why am I getting this error and how should this line be written to avoid the error

I've tried several things but can't seem to get it "right" and don't understand why -

Points mostly for instruction -

Really trying to learn to avoid problems in the future -

Thanks -

Question by:rwinnick
LVL 48

Assisted Solution

Tintin earned 200 total points
ID: 22655716
The error is pretty obvious I would have thought.  The \ and * symbols aren't numeric.  The == test is for numeric tests.  For strings, you should use eq, eg:

if ($first_char eq '*') {

Note that you don't need to backslash the * if it is contained in single quotes.
LVL 85

Assisted Solution

ozo earned 800 total points
ID: 22655721
Did you mean to compare for string equality with eq
instead of for numeric equality with ==
Or did you mean to bind to a regex match with =~
LVL 85

Assisted Solution

ozo earned 800 total points
ID: 22655726
/usr/bin/splain: Reading from STDIN
Argument "*" isn't numeric in numeric eq (==) at ............line 488
Argument "*" isn't numeric in numeric eq (==) at ............line 488 (#1)
    (W numeric) The indicated string was fed as an argument to an operator
    that expected a numeric value instead.  If you're fortunate the message
    will identify which operator was so unfortunate.
Technology Partners: 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!

LVL 39

Accepted Solution

Adam314 earned 1000 total points
ID: 22655730
There are two ways to compare for equality in perl: as a string, or as a number.
To compare as a number, you use the == operator.  eg:    $a == $b
To compare as a string, you use the eq operator.  eg:  $a eq $b

Because your $first_char and '\*' are string, you need to use the eq operator.

Note that '\*' is two characters.  The * character isn't special in a single-quoted string (or in a double-quoted string), so it doesn't need to be escaped.

Also note that another way to determine if a string begins with a particular character (instead of getting the first character, and comparing it to some character) you could use a regex:
    if($str =~ /^\*/) {
        print "str begins with *\n";
    else {
        print "str does not begin with *\n";
LVL 85

Assisted Solution

ozo earned 800 total points
ID: 22655774
As strings, "0", "0.0", "0E0", "-0", "", "\\*" are all unequal
as numbers, they are all equal.
What are you trying to do?
LVL 85

Assisted Solution

ozo earned 800 total points
ID: 22655796
$first_char =~ '\*'
would be true if any character in $first_char was *
$first_char =~ '^\*'
would be true if  $first_char starts with *
$first_char eq '\*'
would be true if length($first_char) == 2 and the first character in $first_char was \ and the second character in  $first_char  was *

Author Comment

ID: 22655807
Thanks everyone -
I have a post that if it has an * as the first character I do one thing and if the first character is not an * I do something else -

So I am trying to get the first character of the post and see if it is an * -

Being VERY NEW at this stuff I didn't realize all the 'possibilities" -

This is all extremely helpful -

I really appreciate the 'instruction' -

Will try it out in my code right now -


Author Comment

ID: 22655829
for a more complete understanding what would not equal to be :-)


Author Comment

ID: 22655902
OK :-)
it works !!
a little learning is a good thing -
Just looked up string inequality eg: ne
I'm on a roll -

Thanks you guys are all great and a  major help

Will close this question with divided points

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

927 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