Force numeric variable !

S

S.Marion

Hello,

I want to compare 2 hexadecinal numbers.
Consider this example:
my $var1 = "0x00d9";
my $var2 = 0x00d9;

if ($var1 != $var2){
print "Yep, you get the point... it's not equal, which is my problem!!\n";
}

The perl runtime complains about this comparison.

How can I force perl to compare?

I tried things like int($var) or $var += 0 but with no luck.

Cheers,

Sebastien
 
P

Paul Lalli

S.Marion said:
I want to compare 2 hexadecinal numbers.
Consider this example:
my $var1 = "0x00d9";
my $var2 = 0x00d9;

You do not have two hexidecimal numbers. You have one hexidecimal
number and one string.
if ($var1 != $var2){
print "Yep, you get the point... it's not equal, which is my problem!!\n";
}

The perl runtime complains about this comparison.

That means you're enabling warnings. Very good!!
How can I force perl to compare?

I tried things like int($var) or $var += 0 but with no luck.

Throwing things at the wall to see what sticks is rarely a good idea
when programming. Instead, read the documentation.

You are obviously having problems with representations of your data.
Therefore `perldoc perldata` seems a likely candidate for documentation
to read which would solve your problem...

==================================================
Scalar value constructors

Numeric literals are specified in any of the following
floating point or integer formats:

12345
12345.67
.23E-10 # a very small number
4_294_967_296 # underline for legibility
0xff # hex
0377 # octal
0b011011 # binary

String literals are usually delimited by either single or
double quotes. They work much like quotes in the standard
Unix shells: double-quoted string literals are subject to
backslash and variable substitution; single-quoted strings
are not (except for "\'" and "\\"). The usual C-style
backslash rules apply for making characters such as newline,
tab, etc., as well as some more exotic forms. See the Quote
and Quote-like Operators entry in the perlop manpage for a
list.

Hexadecimal, octal, or binary, representations in string
literals (e.g. '0xff') are not automatically converted to
their integer representation. The hex() and oct() functions
make these conversions for you. See the hex entry in the
perlfunc manpage and the oct entry in the perlfunc manpage
for more details.
====================================================

Paul Lalli
 
T

Tad McClellan

S.Marion said:
I want to compare 2 hexadecinal numbers.


There is no such thing as a "hexadecimal number",
there is only the concept of "a number".

There are many many *representations* of a particular number:

10 base 10
0A base 16
1010 base 2
5 + 5

Those all represent the *same* number, commonly referred to
as "ten".

Consider this example:
my $var1 = "0x00d9";


var1 does not contain a hexadecimal number.

It contains a string, which will, when used as if it was a number
be auto-converted to the number zero.

my $var2 = 0x00d9;


var2 contains the number known as 217 in base 10.

if ($var1 != $var2){


So here you have:

if (0 != 217 ){
print "Yep, you get the point... it's not equal, which is my problem!!\n";


so you are *supposed* to get not-equal.


How can I force perl to compare?


By converting the string in hex notation into an actual number.

Consider this example:

--------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $var1 = "0x00d9";
my $var2 = 0x00d9;

# treat them as a string
print "var1 ($var1) is ", length($var1), " chars long\n";
print "var2 ($var2) is ", length($var2), " chars long\n";

# treat them as a number
print "var1 as a number is ", 0 + $var1, "\n";
print "var2 as a number is ", 0 + $var2, "\n";

# convert the string in hex notation to a number
if (hex($var1) == $var2){
print "they ARE equal!\n";
}
 
R

Ronald Matthews

There is no such thing as a "hexadecimal number",
there is only the concept of "a number".

Here is a man with his head jammed, way, way, up his ass.
There are many many *representations* of a particular number:

And any number expressed in base 16 is a hexadecimal number, you
dumb ****. Do a google search for "hexadecimal number" and you will
come up with all kinds of definitions from online dictionaries.

Please, stop posting.

cordially, as always,

rm
 
A

Anno Siegel

Paul Lalli said:
You do not have two hexidecimal numbers. You have one hexidecimal
number and one string.

That's still not quite accurate.

There is one number and one string. That the number has been specified in
hexadecimal doesn't rub off on it in any way. It would be the same number
if it had been specified as decimal 217.

I'm a bit pedantic here, but the area is full of misunderstandings that
come from not keeping the notions properly apart. Numbers don't have a
base, representations of numbers do.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top