Question on comparing to variables containing integers

Discussion in 'Perl Misc' started by Huub, Jul 20, 2006.

  1. Huub

    Huub Guest

    Hi,

    I try to read an integer from MySQL and compare it in an if statement:
    if ($var == $compare) {}. The reading from MySQL is fine. The problem I
    have is getting the correct value in $var. What I did when comparing 2
    strings was this:

    @betaald = $sth->fetchrow_array;
    $betaald[0] = @betaald;
    $betaald = $betaald[0];
    $vergelijk = "N";
    if ($betaald == $vergelijk) {..code..}

    I had hoped to use the same handling for an integer, but that doesn't
    work. If say @betaald = 3, then $betaald[0] turns out to be 1, not 3. I
    understand that "1' is the length of @betaald.
    So far, I can't find what I need on CPAN, unless I'm looking for the
    wrong thing.
    Thanks for helping out (hint/clue/link),

    Huub
    Huub, Jul 20, 2006
    #1
    1. Advertising

  2. Huub

    David Squire Guest

    Huub wrote:
    > Hi,
    >
    > I try to read an integer from MySQL and compare it in an if statement:
    > if ($var == $compare) {}. The reading from MySQL is fine. The problem I
    > have is getting the correct value in $var. What I did when comparing 2
    > strings was this:
    >
    > @betaald = $sth->fetchrow_array;
    > $betaald[0] = @betaald;


    You have now written the size of the array @betaald into the first
    element of that array, $betaald[0], overwriting whatever was there. Is
    the what you meant to do?

    > $betaald = $betaald[0];


    Now you copy $betaald[0], which holds the size of the array @betaald,
    into a scalar called $betaald.

    > $vergelijk = "N";


    This is a string.

    > if ($betaald == $vergelijk)


    You are comparing using ==, which is for numerical comparisons, but
    $vergelijk contains a string, which will evaluate as zero in numeric
    context (IIRC).

    > {..code..}
    >
    > I had hoped to use the same handling for an integer, but that doesn't
    > work. If say @betaald = 3


    This means that the array @betaald has three elements. Is that what you
    think it means?

    >, then $betaald[0] turns out to be 1, not 3. I
    > understand that "1' is the length of @betaald.


    Well, I guess @betaald only had one element.

    It's hard to tell from this, but I suspect that what you want is
    something like:

    @betaald = $sth->fetchrow_array;
    if ($betaald[0] == 1234) { do something }

    Though you really should check that @betaald contains something before
    acting on it...


    DS
    David Squire, Jul 20, 2006
    #2
    1. Advertising

  3. Huub

    Ben Morrow Guest

    Quoth David Squire <>:
    > Huub wrote:
    > >
    > > I had hoped to use the same handling for an integer, but that doesn't
    > > work. If say @betaald = 3

    >
    > This means that the array @betaald has three elements. Is that what you
    > think it means?


    No it doesn't.

    if (@betaald == 3) {...}

    checks if the array has 3 elements, but @betaald = 3 is the same as
    @betaald = (3) and gives the array one element with the value 3.

    > >, then $betaald[0] turns out to be 1, not 3. I
    > > understand that "1' is the length of @betaald.

    >
    > Well, I guess @betaald only had one element.


    Well, yes.

    Ben

    --
    Razors pain you / Rivers are damp
    Acids stain you / And drugs cause cramp. [Dorothy Parker]
    Guns aren't lawful / Nooses give
    Gas smells awful / You might as well live.
    Ben Morrow, Jul 20, 2006
    #3
  4. Huub

    David Squire Guest

    Ben Morrow wrote:
    > Quoth David Squire <>:
    >> Huub wrote:
    >>> I had hoped to use the same handling for an integer, but that doesn't
    >>> work. If say @betaald = 3

    >> This means that the array @betaald has three elements. Is that what you
    >> think it means?

    >
    > No it doesn't.
    >
    > if (@betaald == 3) {...}
    >
    > checks if the array has 3 elements, but @betaald = 3 is the same as
    > @betaald = (3) and gives the array one element with the value 3.
    >


    Sure, but I did not interpret the OPs "@betaald = 3" in text as an
    assignment (since there was nothing like that in his code), but as
    short-hand for "@betaald has the value of 3", which I would be prepared
    to bet is what he meant. (Still, I guess it is good practice to
    discourage using code-like things in non=code-like ways).


    DS
    David Squire, Jul 20, 2006
    #4
  5. Huub

    -berlin.de Guest

    David Squire <> wrote in comp.lang.perl.misc:

    > to bet is what he meant. (Still, I guess it is good practice to
    > discourage using code-like things in non=code-like ways).


    ....such as using an equals-sign where a hyphen should go? :)

    Anno
    -berlin.de, Jul 20, 2006
    #5
  6. Huub <> wrote:

    > I try to read an integer from MySQL and compare it in an if statement:
    > if ($var == $compare) {}.



    That should work fine if $var and $compare are indeed numbers.

    If it isn't working then you don't have a problem with the comparison,
    you have a problem with how you put the values into one or both
    of those variables.

    Find out what values you have by inserting a debugging statement
    before that if:

    print "comparing ($var == $compare)\n";


    > The problem I
    > have is getting the correct value in $var. What I did when comparing 2
    > strings was this:
    >
    > @betaald = $sth->fetchrow_array;
    > $betaald[0] = @betaald;



    This stomps over the value of the first element, replacing it with
    the number of elements in the array.


    > $betaald = $betaald[0];
    > $vergelijk = "N";
    > if ($betaald == $vergelijk) {..code..}

    ^^

    "eq" is for comparing strings.

    "==" is for comparing numbers.

    $vergelijk is clearly a string, so you are using the wrong operator,
    which you should already know since you should always enable warnings
    when developing Perl code.


    > I had hoped to use the same handling



    Errr, but it is wrong.

    If you base new stuff on something that is wrong, the new stuff
    will also be wrong.


    > for an integer, but that doesn't
    > work.



    It does exactly what you told it to do.

    If that isn't what you want, then you need to tell it to do something else.


    > If say @betaald = 3, then $betaald[0] turns out to be 1, not 3. I
    > understand that "1' is the length of @betaald.



    If you want the value of the 1st element instead of the number of
    elements then don't stomp over the value of the 1st element with
    the number of elements.


    > Thanks for helping out (hint/clue/link),



    if ($betaald[0] == $vergelijk) {..code..} # if numbers

    if ($betaald[0] eq $vergelijk) {..code..} # if strings


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 20, 2006
    #6
  7. Huub

    Huub Guest


    >
    > If you want the value of the 1st element instead of the number of
    > elements then don't stomp over the value of the 1st element with
    > the number of elements.
    >
    >
    >> Thanks for helping out (hint/clue/link),

    >
    >
    > if ($betaald[0] == $vergelijk) {..code..} # if numbers
    >
    > if ($betaald[0] eq $vergelijk) {..code..} # if strings
    >
    >


    I have made an error in posting. I'm sorry for that. In the comparison
    of strings I do use "eq". The rest of the code is the same. Due to my
    error some assumptions and solutions given by you and the others are
    possibly not accurate.
    You are talking about stomp/not stomp. In CPAN search, I can't find
    this. Could you explain/be more specific?

    Thank you

    Huub
    Huub, Jul 20, 2006
    #7
  8. Huub

    David Squire Guest

    Huub wrote:
    >
    >>
    >> If you want the value of the 1st element instead of the number of
    >> elements then don't stomp over the value of the 1st element with
    >> the number of elements.
    >>
    >>
    >>> Thanks for helping out (hint/clue/link),

    >>
    >>
    >> if ($betaald[0] == $vergelijk) {..code..} # if numbers
    >>
    >> if ($betaald[0] eq $vergelijk) {..code..} # if strings
    >>
    >>

    >
    > I have made an error in posting. I'm sorry for that.


    .... and the lesson is: never ever type code into your post. Only ever
    cut and paste from an actual script you have tested.

    In the comparison
    > of strings I do use "eq". The rest of the code is the same. Due to my
    > error some assumptions and solutions given by you and the others are
    > possibly not accurate.
    > You are talking about stomp/not stomp. In CPAN search, I can't find
    > this. Could you explain/be more specific?
    >


    "stomp" has nothing to do with Perl. It's English, meaning "to step on
    heavily". Tad meant (as I also said) that you had overwritten the value
    in the first position of the array @betaald, which is almost certainly
    the value you were actually interested in.


    DS
    David Squire, Jul 20, 2006
    #8
  9. Huub

    Huub Guest

    >
    > "stomp" has nothing to do with Perl. It's English, meaning "to step on
    > heavily". Tad meant (as I also said) that you had overwritten the value
    > in the first position of the array @betaald, which is almost certainly
    > the value you were actually interested in.
    >
    >


    Tad suggested that I'd put print statements in between to actually see
    the values. I already did that, which is how I discovered this way
    apparently doesn't work for numbers.
    How can I overwrite the value of the first position of @betaald, if I do
    $betaald[0] = @betaald? This overwrites the first position of $betaald.
    If @betaald has the correct value, and the rest of my code doesn't work
    for numbers, how can I get that value into $betaald? I can't do "if
    (@betaald == @vergelijk) {}".

    Thanks,

    Huub
    Huub, Jul 20, 2006
    #9
  10. Huub

    David Squire Guest

    Huub wrote:
    >
    > Tad suggested that I'd put print statements in between to actually see
    > the values. I already did that, which is how I discovered this way
    > apparently doesn't work for numbers.
    > How can I overwrite the value of the first position of @betaald, if I do
    > $betaald[0] = @betaald? This overwrites the first position of $betaald.
    > If @betaald has the correct value, and the rest of my code doesn't work
    > for numbers, how can I get that value into $betaald?


    You don't need to. You can just use that element of the array directly
    (also as I suggested. Did you even read my response??)

    I can't do "if
    > (@betaald == @vergelijk) {}".



    But you can do:

    if ($betaald[0] = @vergelijk) {...}

    Do this straight after the fetch_rowarray (ideally checking that there
    is actually some data there first). All the assignments you were doing
    in between are superfluous.

    I suspect you need to read up on how to use arrays in Perl. See perldoc
    perldata.


    DS
    David Squire, Jul 20, 2006
    #10
  11. Huub

    David Squire Guest

    David Squire wrote:
    >
    >
    > But you can do:
    >
    > if ($betaald[0] = @vergelijk) {...}


    Whoops! Should be:

    if ($betaald[0] == @vergelijk) {...}
    David Squire, Jul 20, 2006
    #11
  12. Huub

    David Squire Guest

    David Squire wrote:
    > David Squire wrote:
    >>
    >>
    >> But you can do:
    >>
    >> if ($betaald[0] = @vergelijk) {...}

    >
    > Whoops! Should be:
    >
    > if ($betaald[0] == @vergelijk) {...}


    Arghh. Just noticed the second part of what you had written. In the
    example from earlier today you had $vergelijk, a scalar. Now you have
    @vergelijk, an array. Which do you actually mean?


    DS
    David Squire, Jul 20, 2006
    #12
  13. Huub

    Paul Lalli Guest

    Huub wrote:
    > >
    > > "stomp" has nothing to do with Perl. It's English, meaning "to step on
    > > heavily". Tad meant (as I also said) that you had overwritten the value
    > > in the first position of the array @betaald, which is almost certainly
    > > the value you were actually interested in.

    >
    > Tad suggested that I'd put print statements in between to actually see
    > the values. I already did that, which is how I discovered this way
    > apparently doesn't work for numbers.


    What "way" of doing *what* doesn't work for numbers?

    > How can I overwrite the value of the first position of @betaald, if I do
    > $betaald[0] = @betaald?


    This question is non-sensical. That line of code *does* overwrite the
    first position of @betaald. It puts the size of @betaald into the
    first position of @betaald.

    > This overwrites the first position of $betaald.


    This statement is also nonsensical. First, you have, until now, not
    mentioned any such variable $betaald. We've been dealing with an array
    @betaald. $betaald is a scalar variable. There is no such thing as
    its "first position". Even assuming that was just a typo, isn't that
    exactly what you just asked how to do?

    > If @betaald has the correct value, and the rest of my code doesn't work
    > for numbers,


    More nonsense. @betaald does not have a "correct value". @betaald is
    an array, and therefore has a list of values.

    Again, what "doesn't work for numbers"?

    > how can I get that value into $betaald?


    Get *what* value into $betaald?! Please read your post before you
    post it. This is almost entirely gibberish.

    > I can't do "if (@betaald == @vergelijk) {}".


    Of course you *can* do that... that compares the sizes of the two
    arrays @betaald and @vergelijk. If the sizes are equal, the if block
    is executed. If not, the if block is skipped. Is that what you want
    to happen?

    NO WHERE in this post have you said what you actually *want* to do, nor
    *why* you want to do it.

    I believe, however, that you have a major misconception about the way
    scalar and array variables work in Perl. Allow me to attempt to
    explain:

    * @betaald is an array variable. It contains a list of values.
    * $betaald is a scalar variable. It contains one single value.
    * @betaald and $betaald have *NOTHING TO DO WITH EACH OTHER*. They are
    completely un-related. Changing @betaald has no effect on $betaald,
    nor vice versa.
    * To access a certain element in @betaald, you append square-brackets
    with the index number between them, and replace the @ with a $.
    Therefore, $betaald[0] is the first element of @betaald. $betaald[1]
    is the second element, etc.
    * Again, $betaald[0] has NOTHING to do with $betaald. The first is an
    element of the array @betaald, and the second is an unrelated scalar
    variable.
    * When you use a Perl array in scalar context (ex, comparing it to a
    scalar, or assigning it to a scalar), it returns the size of that
    array. For example, $foo = @betaald; assigns $foo to have the size of
    @betaald. `if ($bar == @betaald) { } ` checks to see if $bar's value
    is the same as the size of @betaald.
    * Because elements of an array are themselves scalars, assigning to a
    specific element of an array imposes scalar context. If I had an array
    @stuff, and I said $stuff[0] = @betaald, that would assign the size of
    @betaald to be the first element of @stuff. Likewise, saying
    $betaald[0] = @betaald assigns the first element of @betaald to be the
    size of @betaald (thus replacing whatever was already there)

    I hope that helps to clarify Perl's arrays for you, so that you can
    better express to us what it is you're actually *trying* to accomplish.

    Paul Lalli
    Paul Lalli, Jul 20, 2006
    #13
  14. Huub <> wrote:


    [ Please provide a proper attribution in your followups. ]


    >> if ($betaald[0] == $vergelijk) {..code..} # if numbers
    >>
    >> if ($betaald[0] eq $vergelijk) {..code..} # if strings


    > I have made an error in posting.



    Yet again.


    > I'm sorry for that.



    Sure you are.


    > Due to my
    > error some assumptions and solutions given by you and the others are
    > possibly not accurate.



    Yes, so we were all wasting our time trying to help you then.


    > You are talking about stomp/not stomp.
    > Could you explain/be more specific?



    Yes I could.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 20, 2006
    #14
  15. Huub

    Huub Guest

    Sorry for my errors and waisting your precious time on this. I won't
    bother you on this issue anymore. Thank you for (trying to) help me out.
    Huub, Jul 21, 2006
    #15
    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. Nag
    Replies:
    3
    Views:
    521
  2. Elijah Bailey

    comparing long integers

    Elijah Bailey, Jan 22, 2004, in forum: C++
    Replies:
    12
    Views:
    591
    Dan Pop
    Jan 23, 2004
  3. Nicholas

    Comparing char* with integers and characters

    Nicholas, Sep 9, 2003, in forum: C Programming
    Replies:
    13
    Views:
    600
    Nicholas
    Sep 9, 2003
  4. Elijah Bailey

    comparing long integers

    Elijah Bailey, Jan 22, 2004, in forum: C Programming
    Replies:
    11
    Views:
    625
    Dan Pop
    Jan 23, 2004
  5. Holger
    Replies:
    4
    Views:
    260
    Holger
    Oct 9, 2008
Loading...

Share This Page