[問題] can't use string as an array ref

看板Perl作者時間1年前 (2022/07/15 21:11), 編輯推噓0(001)
留言1則, 1人參與, 1年前最新討論串1/1
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 所以來請教一下各路高手,這該怎麼寫才正確 先謝謝了 -- Sent from my iPhone 7 PiTT // PHJCI -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.216.20.62 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Perl/M.1657890698.A.EF2.html

07/19 09:09, 1年前 , 1F
問題是@$ 用hash應該可解 $hash{$1}=split('\s+',$2);
07/19 09:09, 1F
文章代碼(AID): #1YqMUAxo (Perl)
文章代碼(AID): #1YqMUAxo (Perl)