Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > question on "if () "

Reply
Thread Tools

question on "if () "

 
 
Huub
Guest
Posts: n/a
 
      06-18-2006
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
 
Reply With Quote
 
 
 
 
Xicheng Jia
Guest
Posts: n/a
 
      06-18-2006
Huub wrote:
> 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

 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      06-18-2006
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.

--
Dr. Oliver Corff e-mail: http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-18-2006
Huub <"v.niekerk at hccnet.nl"> wrote:

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


--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
 
Reply With Quote
 
Huub
Guest
Posts: n/a
 
      06-18-2006
> 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.
 
Reply With Quote
 
Huub
Guest
Posts: n/a
 
      06-18-2006
> 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.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-18-2006
Huub" <"v.niekerk at hccnet.nl wrote:
> 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



 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-18-2006
Huub" <"v.niekerk at hccnet.nl wrote:
>> 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


 
Reply With Quote
 
Ch Lamprecht
Guest
Posts: n/a
 
      06-18-2006
Jürgen Exner wrote:
> 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
--

perl -e "print scalar reverse q/(E-Mail Removed)/"
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-18-2006
Ch Lamprecht wrote:
> Jürgen Exner wrote:
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments