I am running the following in Perl v5.12.3 on Mac OS X v10.7.2 (Lion):
#!/usr/local/bin/perl
use strict;
use warnings;
use DBI;
my $db = DBI->connect("dbi:SQLite:testdrive.db") or die "Cannot connect: $DBI::errstr";
my @times = ("13:00","14:30","16:00","17:30","19:00","20:30","22:00");
my $counter = 1;
for (my $d = 1; $d < 12; $d++) {
for (my $t = 0; $t < 7; $t++) {
# Weekend days have seven slots, weekdays
# have only four (barring second friday)
if (($d+4) % 7 < 2 || ($t > 3)) {
$db->do("INSERT INTO tbl_timeslot VALUES ($counter, '$times[$t]', $d);");
$counter++;
# Add 4:00 slot for second Friday
} elsif (($d = 9) && ($t = 3)) {
$db->do("INSERT INTO tbl_timeslot VALUES ($counter, '$times[$t]', $d);");
$counter++;
}
}
}
$db->disconnect;
I get a "Found = in conditional, should be == at addtimes.pl line 16" warning, but there's no equal sign on that line. Also, the loop seems to start at $d == 9
. What am I missing?
Line 16:
if (($d+4) % 7 < 2 || ($t > 3)) {
The problem is in your elsif
} elsif (($d = 9) && ($t = 3)) {
^-----------^--------- should be ==
Because the if
statement started on line 16, and the elsif
is part of that statement, that's where the error got reported from. This is an unfortunate limitation of the Perl compiler.
On an unrelated note, it's much nicer to avoid C-style loops when you can:
for my $d ( 1 .. 11 ) {
...
for my $t ( 0 .. 6 ) {
...
}
}
Isn't that prettier? :)
} elsif (($d = 9) && ($t = 3)) {
This line will assign 9
to $d
and 3
to $t
. As the warning says, you probably want this instead:
} elsif (($d == 9) && ($t == 3)) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With