Question on comparing to variables containing integers

H

Huub

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
 
D

David Squire

Huub said:
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
 
B

Ben Morrow

Quoth David Squire said:
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
 
D

David Squire

Ben said:
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
 
A

anno4000

David Squire said:
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
 
T

Tad McClellan

Huub said:
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
 
H

Huub

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
 
D

David Squire

Huub said:
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
 
H

Huub

"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
 
D

David Squire

Huub said:
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
 
D

David Squire

David said:
David said:
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
 
P

Paul Lalli

Huub said:
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
 
T

Tad McClellan

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.
 
H

Huub

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.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top