Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1716762
  • 博文数量: 362
  • 博客积分: 10587
  • 博客等级: 上将
  • 技术积分: 4098
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-10 18:15
文章分类

全部博文(362)

文章存档

2014年(1)

2013年(58)

2011年(115)

2010年(112)

2009年(76)

分类: PERL

2013-05-23 10:39:44


  1. #下面有4个例子,但通常最后一个更常用。
  2. my %owner1 = (
  3.     "name" => "Santa Claus",
  4.     "DOB" => "1882-12-25",
  5. );

  6. my $owner1Ref = %owner1;

  7. my %owner2 = (
  8.     "name" => "Mickey Mouse",
  9.     "DOB" => "1928-11-18",
  10. );

  11. my $owner2Ref = %owner2;

  12. my @owners = ( $owner1Ref, $owner2Ref );

  13. my $ownersRef = @owners;

  14. my %account = (
  15.     "number" => "12345678",
  16.     "opened" => "2000-01-01",
  17.     "owners" => $ownersRef,
  18. );

  1. #显然你不用这么费劲,可以简化为:
  2. my %owner1 = (
  3.     "name" => "Santa Claus",
  4.     "DOB" => "1882-12-25",
  5. );

  6. my %owner2 = (
  7.     "name" => "Mickey Mouse",
  8.     "DOB" => "1928-11-18",
  9. );

  10. my @owners = ( %owner1, %owner2 );

  11. my %account = (
  12.     "number" => "12345678",
  13.     "opened" => "2000-01-01",
  14.     "owners" => @owners,
  15. );


  1. #也可以使用不同的符号声明匿名数组和哈希表。匿名数组使用方括号,匿名哈希表使用大括号。 这样,返回值就是一个对匿名数据结构的引用。细看一下, 返回的 %accountas 与上面相同:
  2. # Braces denote an anonymous hash
  3. my $owner1Ref = {
  4.     "name" => "Santa Claus",
  5.     "DOB" => "1882-12-25",
  6. };

  7. my $owner2Ref = {
  8.     "name" => "Mickey Mouse",
  9.     "DOB" => "1928-11-18",
  10. };

  11. # Square brackets denote an anonymous array
  12. my $ownersRef = [ $owner1Ref, $owner2Ref ];

  13. my %account = (
  14.     "number" => "12345678",
  15.     "opened" => "2000-01-01",
  16.     "owners" => $ownersRef,
  17. );


  1. #或者更短一些(这就是真正用来声明复杂数据结构的形式):
  2. my %account = (
  3.     "number" => "31415926",
  4.     "opened" => "3000-01-01",
  5.     "owners" => [
  6.         {
  7.             "name" => "Philip Fry",
  8.             "DOB" => "1974-08-06",
  9.         },
  10.         {
  11.             "name" => "Hubert Farnsworth",
  12.             "DOB" => "2841-04-09",
  13.         },
  14.     ],
  15. );


  1. say "$account{owners}[0]{ddddd}";
  2. say "$account{owners}[1]{aaaa}";
  3. say "$account{owners}->[0]->{ddddd}";
    say "$account{owners}->[1]->{aaaa}";


阅读(790) | 评论(0) | 转发(0) |
0

上一篇:职业规划2

下一篇:不要引用数组

给主人留下些什么吧!~~