[问题] can't use string as an array ref

楼主: jacky1989   2022-07-15 21:11:36
as title
我有一个档案是这样的
file:
@fruit: apple banana
@area: area1 area2 area3
我的程式这样写
use strict;
my @fruit;
my @area;
open fid,”<file”;
while(<fid>){
if($_ =~ /^\s*(\S*?)\s*:\s*(.*)$/){
@$1=split(‘\s+’,$2);
next;
}
}
已经先以perl -c确认过语法没问题
但在执行时,显示
can't use string as an array ref while strict refs in use
我是不是哪边没注意到?
因为这个用法我以前有用过,确定能用
我有尝试拿掉strict,array一样是empty
所以来请教一下各路高手,这该怎么写才正确
先谢谢了
作者: lgen7604   2022-07-19 09:09:00
问题是@$ 用hash应该可解 $hash{$1}=split('\s+',$2);

Links booklink

Contact Us: admin [ a t ] ucptt.com