Re: [請益] 關於"變數參考"問題

作者: kuangs (kuangs)   2012-12-13 09:06:12
或許是我描述的不夠完全
我原本是想產生 list=1
list=2
list=3
list=4
list=5
但實際上是產生 list=5
list=5
list=5
list=5
list=5
※ 引述《clanguage (C語言)》之銘言:
: 不好意思 從你的 code 看不出要幹嘛..
: 這樣子好嗎ꀠ把資料都放進 newlist 然後
: $targetdomain->{list} 存 newlist ref
: 最後解參考 $targetdomain->{list} 印出你的 12345
: 1 #!/usr/bin/perl
: 2
: 3 @listref = (1,2,3,4,5);
: 4
: 5 foreach my $bb ( @listref )
: 6 {
: 7 push( @newlist, $bb );
: 8 }
: 9 $targetdomain->{list}=\@newlist;
: 10 print @{ $targetdomain->{list} };
: 11
: ※ 引述《kuangs (kuangs)》之銘言:
: : 大家好
: : 目前小弟遇到一個問題
: : 假設我的資料
: : @listref= 1,2,3,4,5,6
: : foreach my $bb ( @listref )
: : {
: : $newlist{list}=$bb;
: : push( @newlist, \%newlist);
: : }
: : $targetdomain->{list}=\@newlist;
: : print $targetdomain;
: : 印出來的結果
: : 我想要的應該是 1 2 3 4 5
: : 實際上卻是 5 5 5 5 5
: : 我知道原因出在哪 應該是參考變數的問題
: : 五個資料都是參考同一個%newlist
: : 而最後一個輸入的數值為5
: : 所以才會印出 五個5
: : 但是我卻不知怎麼解決!! 請各位高手幫幫忙!!
作者: scwg ( )   2012-02-13 09:33:00
你的原程式跑出來跟你說的完全不一樣啊..
作者: qulqul (飄)   2012-02-13 14:24:00
推樓上!print join("\t",1,2,3,4,5),"\n"; <-降勒XD~

Links booklink

Contact Us: admin [ a t ] ucptt.com