PERL conditional equation error

Posted on 2008-10-06
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 50 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 84

Assisted Solution

ozo earned 200 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 84

Assisted Solution

ozo earned 200 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.
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

LVL 39

Accepted Solution

Adam314 earned 250 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 84

Assisted Solution

ozo earned 200 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 84

Assisted Solution

ozo earned 200 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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 …
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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