question on "if () "

Discussion in 'Perl Misc' started by Huub, Jun 18, 2006.

  1. Huub

    Huub Guest

    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
    Huub, Jun 18, 2006
    #1
    1. Advertising

  2. Huub

    Xicheng Jia Guest

    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
    Xicheng Jia, Jun 18, 2006
    #2
    1. Advertising

  3. Huub

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

    --
    Dr. Oliver Corff e-mail: -berlin.de
    Guest, Jun 18, 2006
    #3
  4. Huub

    John Bokma Guest

    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/
    John Bokma, Jun 18, 2006
    #4
  5. Huub

    Huub Guest

    > 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.
    Huub, Jun 18, 2006
    #5
  6. Huub

    Huub Guest

    > 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.
    Huub, Jun 18, 2006
    #6
  7. 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
    Jürgen Exner, Jun 18, 2006
    #7
  8. 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
    Jürgen Exner, Jun 18, 2006
    #8
  9. Huub

    Ch Lamprecht Guest

    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//"
    Ch Lamprecht, Jun 18, 2006
    #9
  10. 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
    Jürgen Exner, Jun 18, 2006
    #10
  11. Huub

    Huub Guest

    >>
    >> 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.
    Huub, Jun 18, 2006
    #11
  12. <-berlin.de> <-berlin.de> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 18, 2006
    #12
  13. Huub

    Guest Guest

    Tad McClellan <> wrote:
    : <-berlin.de> <-berlin.de> wrote:

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

    --
    Dr. Oliver Corff e-mail: -berlin.de
    Guest, Jun 18, 2006
    #13
    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. sean
    Replies:
    1
    Views:
    578
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    639
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    933
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    407
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    823
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page