Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PERL conditional equation error

Posted on 2008-10-06
9
Medium Priority
?
822 Views
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 -

RIchard
0
Comment
Question by:rwinnick
[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
9 Comments
 
LVL 48

Assisted Solution

by:Tintin
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.
0
 
LVL 84

Assisted Solution

by:ozo
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 =~
?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 800 total points
ID: 22655726
splain
/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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Accepted Solution

by:
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";
    }
0
 
LVL 84

Assisted Solution

by:ozo
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?
0
 
LVL 84

Assisted Solution

by:ozo
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 *
0
 

Author Comment

by:rwinnick
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 -

Richard
0
 

Author Comment

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

R
0
 

Author Comment

by:rwinnick
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
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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

688 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