我有perl脚本的这一部分:
my $thread_count = 20
my %QUEUES;
my $current_queue=0;
while(defined($INPUT[$cnt]))
{
while (my @instance = $q1->fetchrow_array)
{
my $walk = "string";
push @{$QUEUES{$current_queue}},$walk;
$current_queue=($current_queue+1)%$thread_count;
}
while (my @instance = $q2->fetchrow_array) {
my $walk = "string";
push @{$QUEUES{$current_queue}},$walk;
$current_queue=($current_queue+1)%$thread_count;
}
}
我试图在一个数组中推送命令,我决定保留一个哈希,因为我认为我可以保持我的生活轻松而不是if(!defined($QUEUES[$current_queue]))$QUEUES[$current_queue]=[];
我使用了Data::Dumper
和一个常规for循环,发现没有为$ QUEUE中的任何键定义,0到$ thread_count-1。这不是教科书自动生活吗?我做错了什么?
答案 0 :(得分:2)
push @{ $QUEUES{$current_queue} }, $walk;
相当于
push @{ $QUEUES{$current_queue} //= [] }, $walk;
如果在$QUEUES{$current_queue}
不存在时执行该语句,则会创建$QUEUES{$current_queue}
,并为其分配对具有一个元素的数组的引用({{1}的副本}})。
因此,如果$walk
为空,则%QUEUES
语句从未执行过。