- 论坛徽章:
- 30
|
- 获取信息的数据结构
- 这里有四个例子,其中最后一个是最有用的:
- 1.
- my $ownersRef = $account{"owners"};
- my @owners = @{ $ownersRef };
- my $owner1Ref = $owners[0];
- my %owner1 = %{ $owner1Ref };
- my $owner2Ref = $owners[1];
- my %owner2 = %{ $owner2Ref };
- print "Account #", $account{"number"}, "\n";
- print "Opened on ", $account{"opened"}, "\n";
- print "Joint owners:\n";
- print "\t", $owner1{"name"}, " (born ", $owner1{"DOB"}, ")\n";
- print "\t", $owner2{"name"}, " (born ", $owner2{"DOB"}, ")\n";
- 2.
- my @owners = @{ $account{"owners"} };
- my %owner1 = %{ $owners[0] };
- my %owner2 = %{ $owners[1] };
- print "Account #", $account{"number"}, "\n";
- print "Opened on ", $account{"opened"}, "\n";
- print "Joint owners:\n";
- print "\t", $owner1{"name"}, " (born ", $owner1{"DOB"}, ")\n";
- print "\t", $owner2{"name"}, " (born ", $owner2{"DOB"}, ")\n";
- 3.
- my $ownersRef = $account{"owners"};
- my $owner1Ref = $ownersRef->[0];
- my $owner2Ref = $ownersRef->[1];
- print "Account #", $account{"number"}, "\n";
- print "Opened on ", $account{"opened"}, "\n";
- print "Joint owners:\n";
- print "\t", $owner1Ref->{"name"}, " (born ", $owner1Ref->{"DOB"}, ")\n";
- print "\t", $owner2Ref->{"name"}, " (born ", $owner2Ref->{"DOB"}, ")\n";
- 4.
- print "Account #", $account{"number"}, "\n";
- print "Opened on ", $account{"opened"}, "\n";
- print "Joint owners:\n";
- print "\t", $account{"owners"}->[0]->{"name"}, " (born ", $account{"owners"}->[0]->{"DOB"}, ")\n";
- print "\t", $account{"owners"}->[1]->{"name"}, " (born ", $account{"owners"}->[1]->{"DOB"}, ")\n";
- 数组与数组引用
- 这个数组有五个要素:
- my @array1 = (1, 2, 3, 4, 5);
- print @array1; # "12345"
- 这个数组,有一个元素(是一个匿名引):
- my @array2 = [1, 2, 3, 4, 5];
- print @array2; # e.g. "ARRAY(0x182c180)"
- 这个标量是一个匿名引用:
- my $array3Ref = [1, 2, 3, 4, 5];
- print $array3Ref; # e.g. "ARRAY(0x22710c0)"
- print @{ $array3Ref }; # "12345"
- print @$array3Ref; # "12345"
复制代码 |
|