Force numeric variable !

Discussion in 'Perl Misc' started by S.Marion, Mar 7, 2006.

  1. S.Marion

    S.Marion Guest

    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
    S.Marion, Mar 7, 2006
    #1
    1. Advertising

  2. S.Marion

    Guest

    "S.Marion" <> wrote:
    > Hello,
    >
    > I want to compare 2 hexadecinal numbers.


    perldoc -f hex

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Mar 7, 2006
    #2
    1. Advertising

  3. S.Marion

    S.Marion Guest

    Thank you :)

    > Xho
    S.Marion, Mar 7, 2006
    #3
  4. S.Marion

    Paul Lalli Guest

    S.Marion wrote:

    > 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
    Paul Lalli, Mar 7, 2006
    #4
  5. S.Marion

    John Bokma Guest

    John Bokma, Mar 7, 2006
    #5
  6. S.Marion <> wrote:

    > 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";
    }
    --------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 7, 2006
    #6
  7. Tad McClellan <> trolled:
    > S.Marion <> wrote:


    > > I want to compare 2 hexadecinal numbers.


    > 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
    Ronald Matthews, Mar 7, 2006
    #7
  8. S.Marion

    Anno Siegel Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > S.Marion wrote:
    >
    > > 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.


    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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Mar 8, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. leeg
    Replies:
    2
    Views:
    481
  2. Replies:
    5
    Views:
    912
    X-Centric
    Jun 30, 2005
  3. darrel
    Replies:
    4
    Views:
    792
    darrel
    Jul 19, 2007
  4. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    936
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  5. frasmus
    Replies:
    1
    Views:
    107
    Mark J. McGinty
    Jan 12, 2007
Loading...

Share This Page