简体   繁体   English

如何在Perl中引用哈希?

[英]How to reference a hash in Perl?

If i have something like this: 如果我有这样的事情:

%hash = {foo => 'bar', foo1=>'bar1',};

or 要么

%hash = (foo => 'bar', foo1=>'bar1',);

or 要么

$hash = {foo => 'bar', foo1=>'bar1',};

or 要么

$hash = (foo => 'bar', foo1=>'bar1',);

How do the above codes differ? 以上代码有何不同? and how do i access their components? 以及如何访问它们的组件?

The (...) sorts out precedence, while {...} creates an hash reference . (...)排序优先级,而{...}创建哈希引用

%hash = {foo => 'bar', foo1=>'bar1',};

Assigns the %hash a single value. %hash分配单个值。 However, hashes are assigned an even-sized list of key-value-pairs. 但是,为散列分配了一个偶数大小的键值对列表。 Your code will use the stringification of the hashref as a key ( HASH(0x123ABC) ), and undef as value. 您的代码将使用hashref的字符串化作为键( HASH(0x123ABC) ),并将undef用作值。 Please use warnings to tell you when you are assigning a non-even-sized list to a hash variable. 当您将非偶数列表分配给哈希变量时,请use warnings来告诉您。

%hash = (foo => 'bar', foo1=>'bar1',);

is correct. 是正确的。 The RHS list is evaluated in list context, and is assigned to the hash variable. 在列表上下文中评估RHS列表,并将其分配给哈希变量。 You can access entries like $hash{foo} . 您可以访问$hash{foo}类的条目。

$hash = {foo => 'bar', foo1=>'bar1',};

is correct. 是正确的。 A hash reference is assigned to a scalar variable. 哈希引用被分配给标量变量。 You can access elements like $hash->{foo} . 您可以访问$hash->{foo}类的元素。 Note the dereferencing arrow which is neccessary to disambiguate hashref access from hash access (you can have variables named both %hash and $hash in the same scope). 请注意必须使用解除引用箭头才能将hashref访问与哈希访问区分开(在同一范围内,您可以同时将变量%hash$hash命名为)。

$hash = (foo => 'bar', foo1=>'bar1',);

evaluates the list on the RHS in scalar context, and will assign the last value. 在标量上下文中评估RHS上的列表,并将分配最后一个值。 So $hash = "bar1" is the same thing. 所以$hash = "bar1"是同一回事。

  1. Assigns a hash reference to a hash. 将哈希引用分配给哈希。 This doesn't make sense. 这没有道理。
  2. Assigns a list to a hash. 将列表分配给哈希。 This is how you normally handle hashes. 这通常是处理哈希的方式。
  3. Assigns a hash reference to a scalar. 将散列引用分配给标量。 This is how you normally handle hash references. 这通常是处理哈希引用的方式。
  4. Assigns a list to a scalar. 将列表分配给标量。 You'll end up with the last value being stored. 您最终将存储最后一个值。

To assess a scalar value in a hash (2): 要评估哈希(2)中的标量值:

$hash{'foo'}

To access a scalar value in a hashref (3): 要在hashref(3)中访问标量值:

$hash->{'foo'}

See perldoc perlref for more about references 有关参考的更多信息,请参见perldoc perlref

You want 你要

my %hash = ( key1 => 'value1', key2 => 'value2' );  # Hash

or 要么

my $hash = { key1 => 'value1', key2 => 'value2' };  # Reference to hash

When one assigns a list to a hash, it is treated as a list of key-value pairs,so the proper way to initialize a hash is 当将一个列表分配给哈希表时,会将其视为键值对列表,因此初始化哈希表的正确方法是

my %hash = ( key1 => 'value1', key2 => 'value2' );

{ ... }

is similar to 类似于

do { my %anon = ( ... ); \%anon }

It creates an anonymous hash and returns a reference to it. 它创建一个匿名哈希并返回对其的引用。 A single reference is not a list of key-value pairs, so the following makes no sense: 单个引用不是键-值对的列表,因此以下内容没有意义:

my %hash = { ... };   # XXX

If you did want to store a reference to a hash, you'd store it in a scalar. 如果您确实想存储对哈希的引用,则可以将其存储在标量中。

my $hash = { ... };   # ok

So what does the following do, then? 那么,接下来的工作是什么呢?

my $hash = ( a => 'b', c => 'd' );

Well, 好,

a => 'b', c => 'd'

is just a fancy way of writing 只是一种奇特的写作方式

'a', 'b', 'c', 'd'

And since we're assigning to a scalar, the right-hand side of the assignment operator is evaluated in scalar context. 而且由于我们要分配给标量,所以赋值运算符的右侧是在标量上下文中求值的。 A list literal (eg 'a', 'b', 'c', 'd' ) in scalar context evaluates in its last item, so 标量上下文中的列表文字(例如'a', 'b', 'c', 'd' )在其最后一项中求值,因此

my $hash = ( a => 'b', c => 'd' );

means 手段

my $hash = 'd';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM