有人能爲我完成此操作並解釋你做了什麼嗎?取消引用散列元素中的列表引用
my %hash;
#$hash{list_ref}=[1,2,3];
#my @[email protected]{$hash{list_ref}};
$hash{list_ref}=\[1,2,3];
my @array=???
print "Two is $array[1]";
有人能爲我完成此操作並解釋你做了什麼嗎?取消引用散列元素中的列表引用
my %hash;
#$hash{list_ref}=[1,2,3];
#my @[email protected]{$hash{list_ref}};
$hash{list_ref}=\[1,2,3];
my @array=???
print "Two is $array[1]";
my %hash;
#$hash{list_ref}=[1,2,3];
#Putting the list in square brackets makes it a reference so you don't need '\'
$hash{list_ref}=[1,2,3];
#If you want to use a '\' to make a reference it is done like this:
# $something = \(1,2,3); # A reference to a list
#
# or (as I did above)...
#
# $something = [1,2,3]; # Returns a list reference
#
# They are the same (for all intent and purpose)
print "Two is $hash{list_ref}->[1]\n";
# To make it completely referenced do this:
#
# $hash = {};
# $hash->{list_ref} = [1,2,3];
#
# print $hash->{list_ref}[1] . "\n";
要獲取數組(數組或列表)做到這一點:
my @array = @{ $hash{list_ref} }
是的,如上所述,這是一個實際問題的簡化版本,所以我不能按你的意願去做。如果你看看我上面的評論代碼,你會看到我已經包含了這個解決方案。 – Hawk 2012-03-27 19:20:10
其實......'\(1,2,3)'和'[1,2,3]'不一樣。 '(1,2,3)'給出'(\ 1,\ 2,\ 3)'。嘗試將其送入'Data :: Dumper'。 – dgw 2012-03-27 19:28:34
dgw是正確的。我立場糾正。我希望不再有一個Perl法庭。我承認:我懶得回去看書。 – user1269651 2012-03-27 19:53:11
@array = @{${$hash{list_ref}}};
(1,2,3)
是一個列表。
[1,2,3]
是
參考列表
的陣列(技術上,有在Perl沒有這樣的東西,以列表的引用)。
\[1,2,3]
是對數組引用的引用。
$hash{list_ref}
是對數組引用的引用。
${$hash{list_ref}}
是對數組的引用。
@{${$hash{list_ref}}}
是一個數組。
由於引用被認爲是標量,因此對引用的引用是標量引用,並且在中間步驟中使用標量解引用操作符${...}
。
[ EXPR ]
創建一個匿名數組,將由EXPR返回的值賦給它,並返回對它的引用。這意味着它幾乎一樣
do { my @anon = (EXPR); \@anon }
這意味着,
\[ EXPR ]
幾乎是一樣的
do { my @anon = (EXPR); \\@anon }
這不是一個正常看到。
換句話說,
1,2,3
返回三個元素(在列表上下文)的列表。
(1,2,3)
與以前相同。 Parens隻影響優先級。
[1,2,3]
返回對包含三個元素的數組的引用。
\[1,2,3]
返回對包含三個元素的數組的引用的引用。
在實踐中:
my @data = (1,2,3);
print @data;
my $data = [1,2,3]; $hash{list_ref} = [1,2,3];
print @{ $data }; print @{ $hash{list_ref} };
my $data = \[1,2,3]; $hash{list_ref} = \[1,2,3];
print @{ ${ $data } }; print @{ ${ $hash{list_ref} } };
其他人幾乎已經回答了這個問題,但更普遍,如果您對數據結構感到困惑,請使用Data::Dumper。這將打印出神祕的數據結構,並幫助您解析它。
use strict; #Always, always, always
use warnings; #Always, always, always
use feature qw(say); #Nicer than 'print'
use Data::Dumper; #Calling in the big guns!
my $data_something = \[1,2,3];
say Dumper $data_something;
say Dumper ${ $data_something };
讓我們來看看它打印出...
$ test.pl
$VAR1 = \[
1,
2,
3
];
$VAR1 = [
1,
2,
3
];
從第一場,看來$data_something
是一個普通的標引用數組引用。這讓我在第一次運行該程序後添加第二個Dumper
。這表明${ $data_something }
現在是對數組的引用。
我現在可以訪問陣列像這樣:
use strict; #Always, always, always
use warnings; #Always, always, always
use feature qw(say); #Nicer than 'print'
use Data::Dumper; #Calling in the big guns!
my $data_something = \[1,2,3];
# Double dereference
my @array = @{ ${ $data_something } }; #Could be written as @$$data_something
for my $element (@array) {
say "Element is $element";
}
而現在......
$ test.pl
Element is 1
Element is 2
Element is 3
它看起來像你的意思是:
my $hash{list_ref} = [1,2,3];
,而不是:
$hash{list_ref} = \[1,2,3];
後者給了你一個數組引用的標量引用,除了給這種情況添加混淆之外,真的不會對你有所幫助。
然後,你所要做的只是指一個特定的元素是$hash{list_ref}->[0]
。這只是${ $hash{list_ref} }[0]
的快捷方式。閱讀和理解起來更容易。
use strict;
use warnings;
use feature qw(say);
my %hash;
$hash{list_ref} = [1, 2, 3];
foreach my $element (0..2) {
say "Element is " . $hash{list_ref}->[$element];
}
而且......
$ test.pl
Element is 1
Element is 2
Element is 3
所以,下一次你無所適從一個特定的數據結構看起來像(和它發生在我們最好的。嗯......不是我們最好的,它發生在我身上),使用Data::Dumper
。
爲什麼要引用數組引用? – cjm 2012-03-27 18:50:45
它實際上有其他代碼餵養它。這只是實際問題的簡化版本。 – Hawk 2012-03-27 18:56:41
這不是一個列表引用,它是一個數組引用。列表和數組在Perl中是不同的。 – 2012-03-28 10:52:33