CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Comparing numbers

 



Ordawg
Deleted

Jan 3, 2001, 1:28 PM

Post #1 of 5 (260 views)
Comparing numbers Can't Post

I have something that looks like this:
if (@info2[8] != "1.1") {
<do something>
}

but if the value in @info2[8] = 1.10 or 1.100 or something like that then it wont do the something that I need it to.

I tried using the "1.1\b" and "1.1\d" but it still didnt work. Please help. Thanks.



rGeoffrey
User / Moderator

Jan 3, 2001, 3:12 PM

Post #2 of 5 (256 views)
Re: Comparing numbers [In reply to] Can't Post

You are using a != to compare numbers, but on the right side you have "1.1", which is a string. So the comparison gets confused.

These should do what you want...

if ( @info2[8] =~ m/1\.1/ ) { }

Note the backslash before the '.' because it is really a '.'.

if ( @info2[8] != 1.1 ) { }

Note the lack of quotes because the right side is a number.




Mortimer
journeyman

Jan 3, 2001, 3:20 PM

Post #3 of 5 (256 views)
Re: Comparing numbers [In reply to] Can't Post

>I have something that looks like this:

>if(@info2[8] != "1.1"){
><do something>
>}

You're trying to access a scalar value in list context.
Try `$info2[8]'.

>but if the value in @info2[8] = 1.10 or 1.100 or something
>like that then it wont do the something that I need it to.

Even if the value of $info2[8] is 1.1, it won't <do something>,
because you're telling it to <do something> if the value is
*not* 1.1. You're `if' condition should return true. Change
the condition to:


Code
unless($info2[8] != 1.1){...

or


Code
if($info2[8] == 1.1){...

and then you can <do something>.

Cheers,

Dave. (Wannabe Perl hacker).
www.dmscripts.com - davemortimer@one.net.au




Mortimer
journeyman

Jan 3, 2001, 4:46 PM

Post #4 of 5 (255 views)
Re: Comparing numbers [In reply to] Can't Post

Re:
rGeoffrey
(journeyman)
01/03/01 05:12 PM

>You are using a != to compare numbers, but on the right
>side you have "1.1", which is a string. So the comparison
>gets confused.

>These should do what you want...

>if (@info2[8] =~ m/1\.1/){ }

>Note the backslash before the '.' because it is really a '.'.

This doesn't look good to me. You're treating a decimal point,
which is used in numeric values, as a full stop (or period as
the Yanks say). The value in $info2[8] is numeric and should,
strictly speaking, be treated as such. That's why it fails.
Try assigning *any* value to $info2[8] with 1.1 in it somewhere,
eg: 531.1234 which is clearly, != 1.1

>if(@info2[8] != 1.1){ }

You are also trying to access a scalar value in list context.
Also, this fails for the same reason as in my first answer to Ordawg.

I think Ordawg should clear something up for us here.
Ordawg, do you wish to treat the values in @info2 as strings,
as is conventional in `version' formatting, or as numbers?
If the former, you should look at eq, ne, string functions
and regexps etc.

Cheers,

Dave. (Wannabe Perl hacker).
www.dmscripts.com - davemortimer@one.net.au




Ordawg
Deleted

Jan 4, 2001, 7:22 AM

Post #5 of 5 (246 views)
Re: Comparing numbers [In reply to] Can't Post

Thank you Mortimer and rGeoffrey for your replies.

I changed the line so that it was comparing strings instead of number and just changed the way that I was storing the value in @info2[8] so that I store a string.

So now I use if (@info2[8] ne "1.1") {} and it works.

Thank you both very much.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives