Testing on empty variable

H

Huub

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

Jürgen Exner

Huub said:
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

Joost Diepenmaat

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

Joost Diepenmaat

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

Huub

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

Huub

Joost said:
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"...
 
M

Michele Dondi

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top