hi,
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Data::Dumper;
5
6 my %index;
7
8 my $year = 2008;
9 my $mon = 02;
10 my $day = 01;
11
12 my $r = [$year,$mon,$day];
13 push (@{$index{$year}},$r);
14
15 $r = [$year,++$mon,++$day];
16 push (@{$index{$year}},$r);
17
18 $r = [++$year,++$mon,++$day];
19 push (@{$index{$year}},$r);
20
21 print Dumper(\%index);
22 print ref(\%index),"\n";
23 print ref($index{$year}),"\n";
:! ./t.pl
$VAR1 = {
'2008' => [
[
2008,
2,
1
],
[
2008,
3,
2
]
],
'2009' => [
[
2009,
4,
3
]
]
};
HASH
ARRAY
我的理解是:
> $rlEntry = [$year, $category, $name];
组成$year,$category,$name组成$rlEntry这个数组引用
> push (@{$year_index {$year}}, $rlEntry);
多个/一个$rlentry再组...@{$year_index{$year}数组而这个数组, 再按$year为 key组成hash.
最后, %category_index就是匿名数组的匿名数组的hash!
2009/1/8 strive_wang <[email protected]>
>
>
>
> 在2009-01-07,"Qiang (James)" <[email protected]> 写道:
> >[email protected] wrote:
> >> open (F, "oscar.txt") || die "Could not open database: $:";
> >> %category_index = (); %year_index = ();
> >> while ($line = <F>) {
> >> chomp $line;
> >> ($year, $category, $name) = split (/:/, $line);
> >> create_entry($year, $category, $name) if $name;
> >> }
> >> sub create_entry { # create_entry (year, category, name)
> >> my($year, $category, $name) = @_;
> >> # Create an anonymous array for each entry
> >> $rlEntry = [$year, $category, $name];
> >> # Add this to the two indices
> >> push (@{$year_index {$year}}, $rlEntry); # By Year
> >> push (@{$category_index{$category}}, $rlEntry); # By Category
> >> }
> >> -----------------------------
> >> 这里push (@{$year_index {$year}}, $rlEntry);
> >> 我的理解是:
> >> $year_index{$year}应该是%year_index的一个元素,但是没有被赋予任何值,那么在之前加...@是什么意思呢?没有变量名的数
> >> 组?但是打印出来的话又看到$year_index{$year}是一个数组的引用,是在无法理解。
> >
> >在 Perl 里你可以给你一个没有初始的 hash key 赋值, Perl 会自动生成 key
> >并把值付给这个 key。
> >
> >这在 Perl 里叫 "autovivification" --- 中文大概是自动繁衍的意思吧。Perl
> >Reference 教程里有提到 http://perldoc.perl.org/perlreftut.html
> >
> >加 @{} 表示 $year_index {$year} 值是一个数组,这样你才可以 push.
> >
> >Qiang(James)
> >
> >>
>
>
>
> ------------------------------
> 《大话西游外传》贺岁新作,送豪宅、送你5000元压岁钱<http://allyes.nie.163.com/main/adfclick?db=afanie&bid=1698,833,77&cid=148,4,1&sid=1804&show=ignore&url=http://xyw.16%0A3.com/2008/heka/>
> >
>
--
Best regards.
Felix New
--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina 论坛”论坛。
要在此论坛发帖,请发电子邮件到 [email protected]
要退订此论坛,请发邮件至 [email protected]
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---