Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Handling both string and numeric comparisons

by perl_devel (Friar)
on May 12, 2005 at 11:28 UTC ( [id://456349]=perlquestion: print w/replies, xml ) Need Help??

perl_devel has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I overcame a problem which displays warnings while executing perl program, the situation is

sub compare{ $param=shift; print "String" if($param eq "5"); }
In the above case the $param may be a string or integer and in case if it is a integer then, we will usually get a warning saying cannot do a string comparison for the integer value .

so can this be avoided ,is there a way to check if $param is integer of string and based on that do a string or integer comparison? Like we do a check for Ref("ARRRAY")?

Regards

CODE tags added by Arunbear

Replies are listed 'Best First'.
Re: Handling both string and numeric comparisons
by holli (Abbot) on May 12, 2005 at 13:00 UTC
    In this code the warning occurs, only at the numeric comparison (==) not the at string (eq). Can you post an example to demonstrate your problem?
    use strict; use warnings; my ($number, $string) = (5, "hello"); print 1 if $number eq $string; #ok print 2 if $number == $string; #Argument "hello" isn't numeric in nume +ric eq (==) at C:\w.pl line 6.


    holli, /regexed monk/
Re: Handling both string and numeric comparisons
by rob_au (Abbot) on May 12, 2005 at 11:31 UTC
    You can force the numeric context for your string in the following fashion:
    if (0 + $param == 5) { ... }
    This will execute and provide the comparison operation that you desire without error or warning. Where $param is a string, the result of the inner operation is zero.

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"

      If you use warnings;, you will get a warning:
      Argument "five" isn't numeric in addition (+) at try.pl line 6.
      You will not get a warning for doing string comparison on a number, though.

      Caution: Contents may have been coded under pressure.
Re: Handling both string and numeric comparisons
by thcsoft (Monk) on May 12, 2005 at 11:34 UTC
    i never read the error message you're reporting. but if it really occours, try to avoid it with a small regex:
    my $isnum = 1 if ($param =~ /^\d+$/);
    another way might work with sprintf().
    however, perl doesn't care for the difference between strings an numbers, as long as you don't try execute inappropriate operations on them.

    language is a virus from outer space.

      Be careful with that construct. What will $isnum be if the regex is false?

Re: Handling both string and numeric comparisons
by Aragorn (Curate) on May 12, 2005 at 14:44 UTC
    In the standard module Scalar::Util, there is a function looks_like_number EXPR with which you can check this. It'll return true if Perl thinks EXPR is a number.

    Arjen

Re: Handling both string and numeric comparisons
by starbolin (Hermit) on May 12, 2005 at 17:45 UTC

    I have to agree with holli. Can you show a piece of code where ( $param eq '5' ) fails? Perl will convert integers to string automatically. The only time I have problems is when I need to force an integer.

    perl_devel writes:

    "is there a way to check if $param is integer of string and based on that do a string or integer comparison?"
    I don't see the purpose. Perl does the integer to string conversion automatically depending on what it thinks it needs. To check for both would require explicitly casting a string to an integer. So, if you are casting it to one or the other, what's the point??

    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://456349]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-19 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found