如何在 Perl 中创建哈希的哈希?

发布于 2024-08-02 13:12:30 字数 344 浏览 6 评论 0原文

我是 Perl 新手。我需要在 Perl 中定义一个如下所示的数据结构:

  city 1 -> street 1 - [ name , no of house , senior people ]
            street 2 - [ name , no of house , senior people ]


  city 2 -> street 1 - [ name , no of house , senior people ]
            street 2 - [ name , no of house , senior people ]

我怎样才能实现这个?

I am new to Perl. I need to define a data structure in Perl that looks like this:

  city 1 -> street 1 - [ name , no of house , senior people ]
            street 2 - [ name , no of house , senior people ]


  city 2 -> street 1 - [ name , no of house , senior people ]
            street 2 - [ name , no of house , senior people ]

How can I acheive this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

海之角 2024-08-09 13:12:30

这是使用哈希引用的另一个示例:

my $data = {
    city1 => {
        street1 => ['name', 'house no', 'senior people'],
        street2 => ['name','house no','senior people'],
    },
    city2 => {
        street1 => etc...
        ...
    }
};

然后您可以通过以下方式访问数据:

$data->{'city1'}{'street1'}[0];

或者:

my @street_data = @{$data->{'city1'}{'street1'}};
print @street_data;

Here is an another example using a hash reference:

my $data = {
    city1 => {
        street1 => ['name', 'house no', 'senior people'],
        street2 => ['name','house no','senior people'],
    },
    city2 => {
        street1 => etc...
        ...
    }
};

You then can access the data the following way:

$data->{'city1'}{'street1'}[0];

Or:

my @street_data = @{$data->{'city1'}{'street1'}};
print @street_data;
等待我真够勒 2024-08-09 13:12:30

答案

my %city ;

 $city{$c_name}{$street} = [ $name , $no_house , $senior];

我找到了可以用这种方式生成的

I found the answer like

my %city ;

 $city{$c_name}{$street} = [ $name , $no_house , $senior];

i can generate in this way

拒绝两难 2024-08-09 13:12:30

Perl 数据结构手册 perldsc 可能会有所帮助。它有示例向您展示如何创建通用数据结构。

The Perl Data Structures Cookbook, perldsc, may be able to help. It has examples showing you how to create common data structures.

海风掠过北极光 2024-08-09 13:12:30
my %city ;

如果你想推

push( @{ city{ $c_name } { $street } }, [ $name , $no_house , $senior] );

(0r)

push @{ city{ $c_name } { $street } }, [ $name , $no_house , $senior];
my %city ;

If you want to push

push( @{ city{ $c_name } { $street } }, [ $name , $no_house , $senior] );

(0r)

push @{ city{ $c_name } { $street } }, [ $name , $no_house , $senior];
笙痞 2024-08-09 13:12:30

您可以在这个答案中阅读我的简短教程。简而言之,您可以将对哈希的引用放入值中。

%hash = ( name => 'value' );
%hash_of_hash = ( name => \%hash );
#OR
$hash_of_hash{ name } =  \%hash;


# NOTICE: {} for hash, and [] for array
%hash2 = ( of_hash => { of_array => [1,2,3] } );
#                  ---^          ---^
$hash2{ of_hash }{ of_array }[ 2 ]; # value is '3'
#     ^-- lack of -> because declared by % and ()


# same but with hash reference
# NOTICE: { } when declare
# NOTICE: ->  when access
$hash_ref = { of_hash => { of_array => [1,2,3] } };
#        ---^
$hash_ref->{ of_hash }{ of_array }[ 2 ]; # value is '3'
#     ---^

You may read my short tutorial at this answer. In short you may put reference to hash into the value.

%hash = ( name => 'value' );
%hash_of_hash = ( name => \%hash );
#OR
$hash_of_hash{ name } =  \%hash;


# NOTICE: {} for hash, and [] for array
%hash2 = ( of_hash => { of_array => [1,2,3] } );
#                  ---^          ---^
$hash2{ of_hash }{ of_array }[ 2 ]; # value is '3'
#     ^-- lack of -> because declared by % and ()


# same but with hash reference
# NOTICE: { } when declare
# NOTICE: ->  when access
$hash_ref = { of_hash => { of_array => [1,2,3] } };
#        ---^
$hash_ref->{ of_hash }{ of_array }[ 2 ]; # value is '3'
#     ---^
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文