加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl警告:“发现=有条件的,应该是==”,但是线上没有等号

发布时间:2020-12-15 23:30:36 所属栏目:大数据 来源:网络整理
导读:在MacOS 10.7.2上的Perl v5.12.3中运行以下命令: #!/usr/local/bin/perluse 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",
在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,$d);");
            $counter++;
        }
    }
}

$db->disconnect;

我得到一个“Found = in conditional,should is == at addtimes.pl line 16”警告,但是那条线上没有等号.此外,循环似乎从$d == 9开始.我错过了什么?

第16行:

if (($d+4) % 7 < 2 || ($t > 3)) {

谢谢.

解决方法

问题在于你的elsif

} elsif (($d = 9) && ($t = 3)) {
             ^-----------^--------- should be ==

因为if语句在第16行开始,并且elsif是该语句的一部分,所以这是报告错误的地方.这是Perl编译器的一个不幸的限制.

在一个不相关的说明中,当你可以避免C风格循环时更好:

for my $d ( 1 .. 11 ) { 
    ...
    for my $t ( 0 .. 6 ) { 
        ...
    }
}

那不漂亮吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读