question on "if () "

H

Huub

Hi,

I want to compare a variable with 1, but the code after "if" is executed
anyway. This is the code immediately before "if":

$bezorger = "select bezorger from hvw where lidnr = $record";
$sth = $dbh->prepare($bezorger);
$sth->execute or die "SQL Error: $DBI::errstr\n";
@bezorger = $sth->fetchrow_array;
print "Record: $record\n";
print "Bezorger: @bezorger\n";
# $bezorger = @bezorger;
if (@bezorger == 1)
{

I did "if ($bezorger == 1)" and "if (@bezorger == 1)" but neither seems
to work. Do I miss something?

Thanks,

Huub
 
X

Xicheng Jia

Huub said:
Hi,

I want to compare a variable with 1, but the code after "if" is executed
anyway. This is the code immediately before "if":

$bezorger = "select bezorger from hvw where lidnr = $record";

select bezorger from hvw where lidnr = $record

coz by the above SQL command, you select only one field from your
table.. so the fetched array @bezorger conatian only one element at
each pass. :)

Xicheng
 
G

Guest

Huub <"v.niekerk at hccnet.nl"> wrote:

: $bezorger = "select bezorger from hvw where lidnr = $record";

Your result will yield one line only.

: # $bezorger = @bezorger;

Let @bezorger contain more than one scalar, then you probably want to say
$bezorger=@bezorger[0].

: I did "if ($bezorger == 1)" and "if (@bezorger == 1)" but neither seems
: to work. Do I miss something?

if ($bezorger == 1) yields true if the scalar $bezorger is exactly equal to
the numerical value 1.

if (@bezorger == 1) yields true if the array @bezorger contains exactly one
element, regardless of the numerical or textual value of that element.

Oliver.
 
J

John Bokma

Huub said:
Hi,

I want to compare a variable with 1, but the code after "if" is executed
anyway. This is the code immediately before "if":

$bezorger = "select bezorger from hvw where lidnr = $record";
$sth = $dbh->prepare($bezorger);
$sth->execute or die "SQL Error: $DBI::errstr\n";
@bezorger = $sth->fetchrow_array;
print "Record: $record\n";
print "Bezorger: @bezorger\n";
# $bezorger = @bezorger;
if (@bezorger == 1)
{

I did "if ($bezorger == 1)" and "if (@bezorger == 1)" but neither seems
to work. Do I miss something?

Your question has already been answered, yet a few tips:

put:

use strict;
use warnings;

on top of your script.

Use prepared statements, especially if $record comes from outside your
script (and check it, if it has an unexpected value, make your script die
with an error).

Understand what @bezorger == 1 does, never try to guess / trial and error.
The problem with that is that you learn guesses, and they might be wrong
in some cases, adding to the confusion. It also results in cluttered code
which is hard to read for someone who did do the hard work.
 
H

Huub

Your result will yield one line only.
: # $bezorger = @bezorger;

Let @bezorger contain more than one scalar, then you probably want to say
$bezorger=@bezorger[0].

@bezorger will contain only 1 element or scalar. A print-call after
$bezorger = @bezorger showed that the number of elements was assigned to
$bezorger, not the value ( 1 instead of 3). Putting [] behind @bezorger
leads to errors. Any more clues?
if ($bezorger == 1) yields true if the scalar $bezorger is exactly equal to
the numerical value 1.

if (@bezorger == 1) yields true if the array @bezorger contains exactly one
element, regardless of the numerical or textual value of that element.

Thank you.
 
H

Huub

Your result will yield one line only.
: # $bezorger = @bezorger;

Let @bezorger contain more than one scalar, then you probably want to say
$bezorger=@bezorger[0].

@bezorger will contain only 1 element or scalar. A print-call after
$bezorger = @bezorger showed that the number of elements was assigned to
$bezorger, not the value ( 1 instead of 3). Putting [] behind @bezorger
leads to errors. Any more clues?

Solved it...thank you.
if ($bezorger == 1) yields true if the scalar $bezorger is exactly equal to
the numerical value 1.

if (@bezorger == 1) yields true if the array @bezorger contains exactly one
element, regardless of the numerical or textual value of that element.

Thank you.
 
J

Jürgen Exner

Huub" <"v.niekerk at hccnet.nl said:
I want to compare a variable with 1, but the code after "if" is
executed anyway. This is the code immediately before "if":
# $bezorger = @bezorger;
if (@bezorger == 1)

This condition will yield true if the array @bezorger contains exactly one
element. Is that what you meant to test?
I did "if ($bezorger == 1)" and "if (@bezorger == 1)" but neither
seems to work. Do I miss something?

Same for the assignment which will assign the lenght of @bezorger to
$bezorger.

If you want to compare an element of @bezorger with 1, then you will have to
specify that element.

jue
 
J

Jürgen Exner

Huub" <"v.niekerk at hccnet.nl said:
Your result will yield one line only.
# $bezorger = @bezorger;

Let @bezorger contain more than one scalar, then you probably want
to say $bezorger=@bezorger[0].

@bezorger will contain only 1 element or scalar. A print-call after
$bezorger = @bezorger showed that the number of elements was assigned
to $bezorger, not the value ( 1 instead of 3).

Yes. That is the intended semantic of using an array in scalar context: it
returns the lenght of that array, i.e. the number of its elements.
Putting [] behind
@bezorger leads to errors. Any more clues?

If you want the value of an element of that array then just select that
element, e.g. @bezorger[5] if you want the sixth element (array indexing
starts at 0).

jue
 
C

Ch Lamprecht

Jürgen Exner said:
Huub" <"v.niekerk at hccnet.nl wrote:
Putting [] behind
@bezorger leads to errors. Any more clues?


If you want the value of an element of that array then just select that
element, e.g. @bezorger[5] if you want the sixth element (array indexing
starts at 0).

$bezorger[5]

if you are using perl5 ;)

Christoph
 
J

Jürgen Exner

Ch said:
Jürgen Exner said:
Huub" <"v.niekerk at hccnet.nl wrote:
Putting [] behind
@bezorger leads to errors. Any more clues?


If you want the value of an element of that array then just select
that element, e.g. @bezorger[5] if you want the sixth element (array
indexing starts at 0).

$bezorger[5]

if you are using perl5 ;)

Ooops, you are absolutely right, of course.
Brainfart on my side.

jue
 
H

Huub

If you want the value of an element of that array then just select
that element, e.g. @bezorger[5] if you want the sixth element (array
indexing starts at 0).

$bezorger[5]

if you are using perl5 ;)

Yes, found that out..thank you for helping out.
 
T

Tad McClellan

you probably want to say
$bezorger=@bezorger[0].


You never want to say that, since you should always enable warnings
when developing Perl code. :)

So you should instead say:

$bezorger = $bezorger[0];

or

($bezorger) = @bezorger; # a "list assignment"
 
G

Guest

: > you probably want to say
: > $bezorger=@bezorger[0].


: You never want to say that, since you should always enable warnings
: when developing Perl code. :)

Oops. Major gaffe from my side. Please accept my apologies,
$bezorger=$bezorger[0] was intended.

Oliver.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top