简体   繁体   English

为什么将Perl哈希值添加为键?

[英]Why is Perl hash value added as a key?

I have a hash my %read_data = (); 我有一个哈希, my %read_data = ();

I am trying to build up keys and values like this 我正在尝试建立这样的键和值

$read_data{"status"} = 0;
$read_data{"suffix"} = "_SP";
$read_data{"consumption"} = 95;

What I am seeing is as follows, and I cannot figure out what I am doing wrong. 我所看到的如下,并且我无法弄清楚自己在做错什么。

Key=status
Key=0
Key=suffix
Key=_SP
Key=consumption
Key=95

I am printing this using 我正在使用打印

for my $k1 (%read_data)
{
  print "Key=".$k1."\n";
}

While it would need you posting your code to make certain, judging by the output you appear to be iterating the hash as though it were an array; 虽然它需要您发布代码来确定,但是从输出来看,您似乎在像对数组一样对哈希进行迭代; Perl will let you do this, but it's almost never what you want. Perl可以让您做到这一点,但这几乎不是您想要的。 Instead, try something like this: 而是尝试这样的事情:

foreach my $key (keys %read_data) {
  my $value = $read_data{$key};
  print "$key = $value\n";
};

There is a relationship between arrays and hashes that you're hitting: 您要击中的数组和散列之间存在关系:

my %hash = ( one => 1, two => 2, three => 3 );

This creates a three member hash with the keys one , two , and `three. 这将创建密钥的三个成员散列onetwo ,和`三人。 So does this: 这样:

my %hash = ( "one", 1, "two", 2, "three", 3 );

In fact, these two lines are exactly the same statement. 实际上,这两行语句完全相同。 The => is a form of syntactic sugar used to highlight the relationship between one value and another. =>是一种语法糖,用于强调一个值和另一个值之间的关系。 Here's the same line again. 这是同一行。 I'm just messing with your brain in this one, but it produces the same hash as before: 我只是在这弄乱你的大脑,但是它会产生与以前相同的哈希值:

my %hash = ( "one", 1 => "two", 2 => "three", 3 );

Here's another way of assigning the same hash: 这是分配相同哈希的另一种方法:

my @array = ( "one", 1, "two", 2, "three", 3 );
my %hash = @array;

And this is also valid too: 这也是有效的:

my @array = %hash;

There's a strong relationship between hashes and arrays in Perl. Perl中的哈希和数组之间有很强的关系。 If you take an array in a hash context, it becomes a hash. 如果您在哈希上下文中使用数组,则它将成为哈希。 If you take a hash in an array context, it becomes an array. 如果在数组上下文中进行哈希处理,则它将成为数组。 For example: 例如:

 mysub (%hash);

sub mysub {
    my %subhash = @_;
    ...
}

This is a valid (although not recommended way) of passing a hash to a subroutine. 这是将哈希传递给子例程的有效方法(尽管不建议这样做)。 The hash is translated into the @_ array which then gets translated back to a hash in the subroutine. 哈希将转换为@_数组,然后将其转换回子例程中的哈希。

Let's take a look at your loop: 让我们看一下您的循环:

for my $k1 (%read_data) {

The (...) is a list/array context, and thus will take your %read_data hash, and present it in a list context with each key followed by its value . (...)是一个列表/数组上下文,因此将获取您的%read_data哈希,并将其显示在列表上下文中,并带有每个及其

There are a few ways to fix this. 有几种方法可以解决此问题。 One is to use the keys to pull out all of the keys in a hash and return an array of the keys. 一种是使用来提取哈希中的所有键并返回键数组。 This is usually combined with sort to sort the keys into some semblance of order. 通常将其与sort结合使用,以将按键分类为某种顺序。

for my $k1 ( sort keys %read_data ) {

Another is to use the each which returns a series of two member arrays with one key and one value. 另一个方法是使用each ,它返回一系列两个带有一个键和一个值的成员数组。

Using a foreach over a hash will get its keys and values sequentially, so don't do that. 在散列上使用foreach会顺序获取其键和值,因此请勿这样做。

This worked for me: 这对我有用:

while (my ($k, $v) = each %read_data) {
    print "$k = $v\n";
}

(I vastly prefer getting hash entries rather than hash keys which then have to be looked up again.) (我非常喜欢获取哈希条目,而不是哈希密钥 ,然后必须再次查找。)

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

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