Solved

PERL conditional equation error

Posted on 2008-10-06
9
818 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
9 Comments
 
LVL 48

Assisted Solution

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

Assisted Solution

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

Assisted Solution

by:ozo
ozo earned 200 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 39

Accepted Solution

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

Assisted Solution

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

Assisted Solution

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTP Error 502.2 - Bad Gateway 3 222
batch script for automated email 12 123
Regex rule to match two different url 5 75
Create an automated page index 9 53
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

820 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