Testing on empty variable

Discussion in 'Perl Misc' started by Huub, Feb 9, 2008.

  1. Huub

    Huub Guest

    Hi,

    I'm reading records from an MySQL database in order to print some of
    them. The reading works well. Next is that I want to skip the empty
    records. In CPAN I searched for "if" and found the "switch" statement:

    $naam = "select naam from hvw where lidnr = $record";
    $sth = $dbh->prepare($naam);
    $sth->execute or die "SQL Error: $DBI::errstr\n";
    @naam = $sth->fetchrow_array;
    switch (@naam)
    {
    case " " { $record++; print "empty record: $record\n"; }
    else ( read the rest of the fields from the database and print them }
    }

    Problem is that it seems to ignore the 'case " "', because everything is
    printed. Where do I go wrong?

    Thanks.
    Huub, Feb 9, 2008
    #1
    1. Advertising

  2. Huub <"v.niekerk at hccnet.nl"> wrote:
    >Hi,
    >
    >I'm reading records from an MySQL database in order to print some of
    >them. The reading works well. Next is that I want to skip the empty
    >records. In CPAN I searched for "if" and found the "switch" statement:
    >
    > $naam = "select naam from hvw where lidnr = $record";
    > $sth = $dbh->prepare($naam);
    > $sth->execute or die "SQL Error: $DBI::errstr\n";
    > @naam = $sth->fetchrow_array;
    > switch (@naam)
    > {
    > case " " { $record++; print "empty record: $record\n"; }
    > else ( read the rest of the fields from the database and print them }
    > }


    Is there anything wrong with a simple

    for (@naam){
    if ($_ eq '')
    {$record++; print "empty record: $record\n";
    } else { read the rest of the fields from the database and
    print them }
    }
    }

    jue
    Jürgen Exner, Feb 9, 2008
    #2
    1. Advertising

  3. Huub <"v.niekerk at hccnet.nl"> writes:

    Hi Huub,

    I'm assuming you're using the CPAN Switch module.

    > switch (@naam)


    switch() takes a single scalar argument, not a list or array (though it
    may be an array reference). You probably want to loop over each element
    in @naam (or maybe not, it depends on what's in @naam):

    #!perl
    use strict;
    use warnings;
    use Switch;

    my @naam = (' ', 'something', 'else');

    foreach my $nam (@naam) {
    switch($nam) {
    case " " { print "space\n" }
    else { print "Something else: $nam\n" }
    }
    }


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Feb 9, 2008
    #3
  4. Oh, and note that Switch is notorious for causing very unintuitive
    bugs. Personally I wouldn't use it for anything serious and take the
    occasional ugly if .. elsif .. else construct.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Feb 9, 2008
    #4
  5. Huub

    Huub Guest

    > Is there anything wrong with a simple
    >
    > for (@naam){
    > if ($_ eq '')
    > {$record++; print "empty record: $record\n";
    > } else { read the rest of the fields from the database and
    > print them }
    > }
    > }


    Nothing wrong with this. Thank you very much.
    Huub, Feb 9, 2008
    #5
  6. Huub

    Huub Guest

    Joost Diepenmaat wrote:
    > Oh, and note that Switch is notorious for causing very unintuitive
    > bugs. Personally I wouldn't use it for anything serious and take the
    > occasional ugly if .. elsif .. else construct.
    >


    I prefer to use "if"...
    Huub, Feb 9, 2008
    #6
  7. On Sat, 09 Feb 2008 20:54:23 +0100, Joost Diepenmaat <>
    wrote:

    >Oh, and note that Switch is notorious for causing very unintuitive
    >bugs. Personally I wouldn't use it for anything serious and take the
    >occasional ugly if .. elsif .. else construct.


    Or use 5.10's *real* switch a.k.a. C<given>.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Feb 11, 2008
    #7
    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. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,008
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    780
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    561
    Dave Thompson
    Feb 14, 2004
  4. Brian Roberts

    empty lists vs empty generators

    Brian Roberts, May 3, 2005, in forum: Python
    Replies:
    12
    Views:
    639
    Jeremy Bowers
    May 4, 2005
  5. ButlerDJIAM
    Replies:
    0
    Views:
    544
    ButlerDJIAM
    Nov 9, 2006
Loading...

Share This Page