简体   繁体   English

Perl:如何访问哈希中的数组

[英]Perl: How to access an array in a hash

I have a sample hash with an array inside, but it seems wrong the way I access the elements of the array. 我有一个内部带有数组的示例哈希,但是访问数组元素的方式似乎不对。 I do it this way: 我这样做:

%Hash_Object = (
  "Property1","value-1",
  "Property2",("value-2","value-3")
);

print $Hash_Object{Property2}[1];
#blank output!

It is supposed to print out "value-3", but it doesn't. 它应该打印出“ value-3”,但不是。

You do not have an array in your hash. 您的哈希中没有数组。 You have a list. 你有一个清单。 Keep the following in mind: 请记住以下几点:

  • Lists are not the same as arrays in Perl 列表与Perl中的数组不同
  • Lists are flat structures 列表是平面结构
  • Arrays are lists 数组是列表

If you put an array into a list, it will be treated as another list, and lists are flat: 如果将数组放入列表中,它将被视为另一个列表,并且列表是平坦的:

(1, 2, (3, 4, 5), (6, (7)))

is equal to 等于

(1, 2, 3, 4, 5, 6, 7)

If you want to build more complex data structures, you need to use references . 如果要构建更复杂的数据结构,则需要使用reference There are two ways to make a reference. 有两种参考方法。 You can either reference a variable by using \\ like this 您可以像这样使用\\来引用变量

my @foo = qw(a b c);
my $ref = \@foo;

or by constructing it directly as an anonymous reference that you then assign to a variable. 或者直接将其构造为匿名引用,然后将其分配给变量。

my $ref = [ qw(a b c) ];
my $ref2 = [ 1, 2, 3 ];

To make a hash reference, use curly braces {} . 要进行哈希引用,请使用花括号{}

my $ref = { a => 1, b => 2 };

References are scalar values, so they are themselves just a single flat value. 引用是标量值,因此它们本身只是一个单一的值。 That's why you need to dereference them in order to get to the value that's inside of them (really it's not inside, it's referenced ). 这就是为什么您需要取消引用它们才能获得它们内部的值(实际上它不在内部,而是被引用了 )。

%Hash_Object = (
  "Property1","value-1",
  "Property2",["value-2","value-3"]
);
$Hash_Object{Property2}[1];
$Hash_Object{Property2}->[1]; # or like this with ->

You already knew how to do that. 您已经知道该怎么做。 You can also use the -> operator in front of every new dereference. 您还可以在每个新的取消引用前面使用->运算符。 Some people find that clearer to read. 有些人觉得阅读起来更清晰。

For more information, see perlreftut and perlref as well as Mike Friedman's excellent blog post about lists and arrays . 有关更多信息,请参见perlreftut和perlref以及Mike Friedman关于列表和数组的出色博客文章


Your example is not very well written code. 您的示例编写的代码不是很好。 Here are some improvement. 这是一些改进。

  • variable names should be lower-case 变量名应小写
  • use the fat comma => for hash assignments 使用胖逗号 =>进行哈希分配
  • you don't need double quotes "" if you're not interpolating 如果不进行插值,则不需要双引号""
  • always put commas after the final element 总是在最后一个元素后面加上逗号
  • don't name things for what type they are, name them for what they represent 不要以事物的类型来命名事物,以事物的名称来命名事物
  • a hash is not an object 哈希不是对象
  • you need to use my when declaring a new variable 在声明新变量时需要使用my

my %example = (
    Property1 => 'value-1',
    Property2 => [
        'value-2',
        'value-3',
    ],
);

Always use use warnings; 始终使用使用警告; and use strict; 使用严格; in top of the program. 在程序顶部

If you use this it display the following errors 如果使用此选项,则会显示以下错误

Odd number of elements in hash assignment at array.pl line 3.
Can't use string ("value-2") as an ARRAY ref while "strict refs" in use at array.pl line 8

In perl, where list are flatten together . 在perl中,列表统一在一起

so the first error is 所以第一个错误是

Odd number of elements in hash assignment at array.pl line 3

Hashes must has pairs of keys and value. 哈希必须具有成对的键和值。 So the elements of the hash should not be an odd number. 因此,哈希的元素不应为奇数。

Your code should be 您的代码应为

use warnings;
use strict;
my %Hash_Object = (
  "Property1"=>["value-1"],
  "Property2"=>["value-2","value-3"]
);

print $Hash_Object{Property2}[1];

Array values should be in square brackets, thanks simbabque 数组值应放在方括号中,谢谢simbabque

%Hash_Object = (
  "Property1","value-1",
  "Property2",["value-2","value-3"]
);

print $Hash_Object{Property2}[1];

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

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