niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
Question on comparing to variables containing integers
Post new topic   Reply to topic Page 1 of 1 [15 Posts] View previous topic :: View next topic
Author Message
huub
*nix forums addict


Joined: 18 Jan 2006
Posts: 51

PostPosted: Thu Jul 20, 2006 11:02 am    Post subject: Question on comparing to variables containing integers Reply with quote

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
Back to top
David Squire
*nix forums Guru Wannabe


Joined: 08 Apr 2006
Posts: 197

PostPosted: Thu Jul 20, 2006 11:14 am    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Huub wrote:
Quote:
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?

Quote:
$betaald = $betaald[0];

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

Quote:
$vergelijk = "N";

This is a string.

Quote:
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).

Quote:
{..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?

Quote:
, 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
Back to top
Ben Morrow
*nix forums Guru Wannabe


Joined: 24 Apr 2006
Posts: 193

PostPosted: Thu Jul 20, 2006 11:50 am    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Quoth David Squire <David.Squire@no.spam.from.here.au>:
Quote:
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.

Quote:
, 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. benmorrow@tiscali.co.uk
Back to top
David Squire
*nix forums Guru Wannabe


Joined: 08 Apr 2006
Posts: 197

PostPosted: Thu Jul 20, 2006 11:57 am    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Ben Morrow wrote:
Quote:
Quoth David Squire <David.Squire@no.spam.from.here.au>:
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
Back to top
anno4000@radom.zrz.tu-ber
*nix forums beginner


Joined: 05 Jul 2006
Posts: 40

PostPosted: Thu Jul 20, 2006 12:20 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

David Squire <David.Squire@no.spam.from.here.au> wrote in comp.lang.perl.misc:

Quote:
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
Back to top
Tad McClellan
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1647

PostPosted: Thu Jul 20, 2006 12:25 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Huub <> wrote:

Quote:
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";


Quote:
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.


Quote:
$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.


Quote:
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.


Quote:
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.


Quote:
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.


Quote:
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
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Back to top
huub
*nix forums addict


Joined: 18 Jan 2006
Posts: 51

PostPosted: Thu Jul 20, 2006 7:32 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Quote:

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
Back to top
David Squire
*nix forums Guru Wannabe


Joined: 08 Apr 2006
Posts: 197

PostPosted: Thu Jul 20, 2006 7:37 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Huub wrote:
Quote:


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
Quote:
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
Back to top
huub
*nix forums addict


Joined: 18 Jan 2006
Posts: 51

PostPosted: Thu Jul 20, 2006 7:50 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Quote:

"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
Back to top
David Squire
*nix forums Guru Wannabe


Joined: 08 Apr 2006
Posts: 197

PostPosted: Thu Jul 20, 2006 7:56 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Huub wrote:
Quote:

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
Quote:
(@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
Back to top
David Squire
*nix forums Guru Wannabe


Joined: 08 Apr 2006
Posts: 197

PostPosted: Thu Jul 20, 2006 7:57 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

David Squire wrote:
Quote:


But you can do:

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

Whoops! Should be:

if ($betaald[0] == @vergelijk) {...}
Back to top
David Squire
*nix forums Guru Wannabe


Joined: 08 Apr 2006
Posts: 197

PostPosted: Thu Jul 20, 2006 7:58 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

David Squire wrote:
Quote:
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
Back to top
Paul Lalli
*nix forums Guru


Joined: 10 Jun 2005
Posts: 1089

PostPosted: Thu Jul 20, 2006 8:21 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Huub wrote:
Quote:

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

Quote:
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.

Quote:
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?

Quote:
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"?

Quote:
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.

Quote:
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
Back to top
Tad McClellan
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1647

PostPosted: Thu Jul 20, 2006 8:42 pm    Post subject: Re: Question on comparing to variables containing integers Reply with quote

Huub <> wrote:


[ Please provide a proper attribution in your followups. ]


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

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

I have made an error in posting.


Yet again.


Quote:
I'm sorry for that.


Sure you are.


Quote:
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.


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


Yes I could.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Back to top
huub
*nix forums addict


Joined: 18 Jan 2006
Posts: 51

PostPosted: Fri Jul 21, 2006 5:26 am    Post subject: Re: Question on comparing to variables containing integers Reply with quote

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.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [15 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 2:02 pm | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Newbie question: How to forward a domain to a mailbox? leei Postfix 0 Fri Aug 24, 2007 4:55 pm
No new posts configuration question for httpd Karl Wang Apache 1 Fri Jul 21, 2006 2:10 pm
No new posts nim problem/question Ron AIX 0 Fri Jul 21, 2006 1:57 pm
No new posts question for JAVA developer who r using postgres sql as b... deepak pal PostgreSQL 1 Fri Jul 21, 2006 9:00 am
No new posts Encryption Question dtuttle1@gmail.com Berkeley DB 2 Thu Jul 20, 2006 10:09 pm

Mobile Phones | Loans | Internet Advertising | Myspace Layouts | Mobile Phones
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2781s ][ Queries: 16 (0.1271s) ][ GZIP on - Debug on ]