在MacOS 10.7.2上的Perl v5.12.3中运行以下命令:
#!/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 7 slots, weekdays have only 4 (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;
Run Code Online (Sandbox Code Playgroud)
我得到一个"Found = in conditional,should is == at addtimes.pl line 16"警告,但是那条线上没有等号.此外,循环似乎开始于$d == 9.我错过了什么?
第16行:
if (($d+4) % 7 < 2 || ($t > 3)) {
Run Code Online (Sandbox Code Playgroud)
谢谢.
fri*_*edo 18
问题出在你的身上 elsif
} elsif (($d = 9) && ($t = 3)) {
^-----------^--------- should be ==
Run Code Online (Sandbox Code Playgroud)
因为if语句从第16行开始,并且elsif是该语句的一部分,所以这是报告错误的地方.这是Perl编译器的一个不幸的限制.
在一个不相关的说明中,当你可以避免C风格循环时更好:
for my $d ( 1 .. 11 ) {
...
for my $t ( 0 .. 6 ) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
那不漂亮吗?:)
} elsif (($d = 9) && ($t = 3)) {
Run Code Online (Sandbox Code Playgroud)
这条线将分配9给$d和3给$t.正如警告所说,你可能想要这个:
} elsif (($d == 9) && ($t == 3)) {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2324 次 |
| 最近记录: |