亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: Perl引用(已經(jīng)解決) [打印本頁(yè)]

作者: xiaoquqi    時(shí)間: 2007-08-06 12:30
標(biāo)題: Perl引用(已經(jīng)解決)

  1. #!/usr/bin/perl
  2. use strict;
  3. use Data::Dumper;
  4. my $hashs;
  5. $hashs->{'abc'}->{'thanks'} = 1;
  6. $hashs->{'bcd'}->{'good'} = 2;

  7. my @item = (1,2,3);

  8. my $results;

  9. foreach my $item (@item){
  10.   $results->{$item}->{product} = $hashs;
  11. }
  12. print Dumper($results);
復(fù)制代碼

結(jié)果:
$VAR1 = {
          '1' => {
                   'product' => {
                                  'bcd' => {
                                             'good' => 2
                                           },
                                  'abc' => {
                                             'thanks' => 1
                                           }
                                }
                 },
          '3' => {
                   'product' => $VAR1->{'1'}{'product'}
                 },
          '2' => {
                   'product' => $VAR1->{'1'}{'product'}
                 }
        };
為什么會(huì)出現(xiàn)'product' => $VAR1->{'1'}{'product'} 這種?如果$hashs的值是變化的,這樣就得不到正確的結(jié)果了呀
費(fèi)解中,希望各位高手幫忙

解釋:
  1. $results->{$item}->{product} = $hashs;
復(fù)制代碼

這是將hashs地址賦值給$results->{$item}->{product},所以在以后的賦值中,所有的變量都指向了同一地址,才有了上面的情況
解決的方法可以遍歷$hashs進(jìn)行賦值

在精華區(qū)關(guān)于引用的文章中,提到引用的copy可以用
$new_hashs = {%{$hashs}};

[ 本帖最后由 xiaoquqi 于 2007-8-8 10:56 編輯 ]
作者: flw    時(shí)間: 2007-08-06 12:36
因?yàn)槟隳鞘莻(gè)引用。
不這么寫(xiě)還怎么寫(xiě)?
作者: xiaoquqi    時(shí)間: 2007-08-06 12:41

  1. #!/usr/bin/perl
  2. use strict;
  3. use Data::Dumper;
  4. my $hashs;
  5. $hashs->{'abc'}->{'thanks'} = 1;
  6. $hashs->{'bcd'}->{'good'} = 2;

  7. my @item = (1,2,3);

  8. my $results;

  9. foreach my $item (@item){
  10.   if($item == 2){
  11.       $hashs->{'abc'}->{'thanks'} = 3;
  12.   }
  13.   $results->{$item}->{product} = $hashs;
  14. }
  15. print Dumper($results);
復(fù)制代碼

得到的結(jié)果是:
$VAR1 = {
          '1' => {
                   'product' => {
                                  'bcd' => {
                                             'good' => 2
                                           },
                                  'abc' => {
                                             'thanks' => 3
                                           }
                                }
                 },
          '3' => {
                   'product' => $VAR1->{'1'}{'product'}
                 },
          '2' => {
                   'product' => $VAR1->{'1'}{'product'}
                 }
        };

但其實(shí)我想得到的是:
          '1' => {
                   'product' => {
                                  'bcd' => {
                                             'good' => 2
                                           },
                                  'abc' => {
                                             'thanks' => 1
                                           }
                                }
                 },
          '2' => {
                   'product' => {
                                  'bcd' => {
                                             'good' => 2
                                           },
                                  'abc' => {
                                             'thanks' => 3
                                           }
                                }
                 },
……
費(fèi)解……多謝幫忙了……
作者: xiaoquqi    時(shí)間: 2007-08-06 13:20
是不是因?yàn)閔ash 引用地址相同的原因?
作者: flw    時(shí)間: 2007-08-06 13:21
看文檔
看精華
看有關(guān)引用的資料
作者: diancn    時(shí)間: 2007-08-06 15:23
也許你需要的是這個(gè):
  1. #!/usr/bin/perl

  2. use strict;
  3. use Data::Dumper;
  4. my $hashs;
  5. $hashs->[1]->{'abc'}->{'thanks'} = 1;
  6. $hashs->[1]->{'bcd'}->{'good'} = 2;

  7. $hashs->[2]->{'abc'}->{'thanks'} = 2;
  8. $hashs->[2]->{'bcd'}->{'good'} = 2;

  9. $hashs->[3]->{'abc'}->{'thanks'} = 3;
  10. $hashs->[3]->{'bcd'}->{'good'} = 2;

  11. my @item = (1,2,3);

  12. my $results;

  13. foreach my $item (@item){
  14.   $results->{$item}->{product} = $hashs->[$item];
  15.   }
  16. print Dumper($results);
復(fù)制代碼

作者: xiaoquqi    時(shí)間: 2007-08-08 10:56
Thanks 樓上的兄弟 謝謝flw 問(wèn)題已經(jīng)解決
解釋在1樓




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2