免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3124 | 回复: 4
打印 上一主题 下一主题

"$$temp{$array[1]}{$array[2]}=$lineNumber++;" [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-17 09:57 |只看该作者 |倒序浏览
$lineNumber=1;
$_=chr1        2200991         2202468 104 49        2.57727057215024e-24;
my @array=split(/\s/,$_);
my $temp=$array[0];
$$temp{$array[1]}{$array[2]}=$lineNumber++;

"$$temp{$array[1]}{$array[2]}=$lineNumber++;"
是什么意思啊
求指导

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
2 [报告]
发表于 2012-07-17 10:13 |只看该作者
perldoc perlref 里面的 Using References 一节讲了四个规则应该足够了
1.

    Anywhere you'd put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a simple scalar variable containing a reference of the correct type:

        $bar = $$scalarref;
        push(@$arrayref, $filename);
        $$arrayref[0] = "January";
        $$hashref{"KEY"} = "VALUE";
        &$coderef(1,2,3);
        print $globref "output\n";

    It's important to understand that we are specifically not dereferencing $arrayref[0] or $hashref{"KEY"} there. The dereference of the scalar variable happens before it does any key lookups. Anything more complicated than a simple scalar variable must use methods 2 or 3 below. However, a "simple scalar" includes an identifier that itself uses method 1 recursively. Therefore, the following prints "howdy".

        $refrefref = \\\"howdy";
        print $$$$refrefref;

2.

    Anywhere you'd put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a BLOCK returning a reference of the correct type. In other words, the previous examples could be written like this:

        $bar = ${$scalarref};
        push(@{$arrayref}, $filename);
        ${$arrayref}[0] = "January";
        ${$hashref}{"KEY"} = "VALUE";
        &{$coderef}(1,2,3);
        $globref->print("output\n"); # iff IO::Handle is loaded

    Admittedly, it's a little silly to use the curlies in this case, but the BLOCK can contain any arbitrary expression, in particular, subscripted expressions:

        &{ $dispatch{$index} }(1,2,3);        # call correct routine

    Because of being able to omit the curlies for the simple case of $$x , people often make the mistake of viewing the dereferencing symbols as proper operators, and wonder about their precedence. If they were, though, you could use parentheses instead of braces. That's not the case. Consider the difference below; case 0 is a short-hand version of case 1, not case 2:

        $$hashref{"KEY"} = "VALUE";        # CASE 0
        ${$hashref}{"KEY"} = "VALUE";        # CASE 1
        ${$hashref{"KEY"}} = "VALUE";        # CASE 2
        ${$hashref->{"KEY"}} = "VALUE";        # CASE 3

    Case 2 is also deceptive in that you're accessing a variable called %hashref, not dereferencing through $hashref to the hash it's presumably referencing. That would be case 3.
3.

    Subroutine calls and lookups of individual array elements arise often enough that it gets cumbersome to use method 2. As a form of syntactic sugar, the examples for method 2 may be written:

        $arrayref->[0] = "January"; # Array element
        $hashref->{"KEY"} = "VALUE"; # Hash element
        $coderef->(1,2,3); # Subroutine call

    The left side of the arrow can be any expression returning a reference, including a previous dereference. Note that $array[$x] is not the same thing as $array->[$x] here:

        $array[$x]->{"foo"}->[0] = "January";

    This is one of the cases we mentioned earlier in which references could spring into existence when in an lvalue context. Before this statement, $array[$x] may have been undefined. If so, it's automatically defined with a hash reference so that we can look up {"foo"} in it. Likewise $array[$x]->{"foo"} will automatically get defined with an array reference so that we can look up [0] in it. This process is called autovivification.

    One more thing here. The arrow is optional between brackets subscripts, so you can shrink the above down to

        $array[$x]{"foo"}[0] = "January";

    Which, in the degenerate case of using only ordinary arrays, gives you multidimensional arrays just like C's:

        $score[$x][$y][$z] += 42;

    Well, okay, not entirely like C's arrays, actually. C doesn't know how to grow its arrays on demand. Perl does.
4.

    If a reference happens to be a reference to an object, then there are probably methods to access the things referred to, and you should probably stick to those methods unless you're in the class package that defines the object's methods. In other words, be nice, and don't violate the object's encapsulation without a very good reason. Perl does not enforce encapsulation. We are not totalitarians here. We do expect some basic civility though.


论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
3 [报告]
发表于 2012-07-17 10:13 |只看该作者
回复 1# hayida


    use Data:umper;
    print Dumper $temp;

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
4 [报告]
发表于 2012-07-17 10:33 |只看该作者
规则一讲了替换的原则,二讲了大括号的用法,三讲了-> 的用法,四讲了 OO

$$temp{$array[1]}{$array[2]}
=> $$temp{$array[1]}->{$array[2]} #因为规则3可以省略掉 -> ; 而且可知它左边必定是个引用,->右边的 {} 决定了是 hashref
所以 $$temp{$array[1]} 返回一个 hashref 由规则1 可知 $temp 是个引用,这里的 {} 索引的语法决定了 $temp 是 hashref
结合规则3 $$temp{$array[1]} 其实是和 $temp->{$array[1]} 等价的,所以整个表达式也可以重写为

$temp->{$array[1]}->{$array[2]}

论坛徽章:
0
5 [报告]
发表于 2012-07-17 10:44 |只看该作者
这么写看着真累,加点箭头吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP