在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)
>
>>

--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina 论坛”论坛。
 要在此论坛发帖,请发电子邮件到 [email protected]
 要退订此论坛,请发邮件至 [email protected]
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

回复