我应该如何在多级 Perl 哈希中存储值?

发布于 2024-09-27 07:43:45 字数 364 浏览 0 评论 0原文

我想做这样的事情。我记得在这样编程时,我遇到了一些价值观消失的问题。这种类型的结构对于哈希来说“正确/有效”吗?

my %VAR;
$VAR{SCALAR} = "test scalar";
$VAR{ARRAY}[0] = "test array";
$VAR{HASH}{NAME}[0] = "test hash array 1";
$VAR{HASH}{NAME}[1] = "test hash array 2";
$VAR{HASH}{NAME}[2]{SOMEHASH} = "test hash array hash 1";
$VAR{HASH}{NAME}[2]{ANOTHERHASH} = "test hash array hash 2";

I am looking to do something like this. I remember I had some issues with values disappearing when programming like this. Is this type of structure "correct/valid" for a hash?

my %VAR;
$VAR{SCALAR} = "test scalar";
$VAR{ARRAY}[0] = "test array";
$VAR{HASH}{NAME}[0] = "test hash array 1";
$VAR{HASH}{NAME}[1] = "test hash array 2";
$VAR{HASH}{NAME}[2]{SOMEHASH} = "test hash array hash 1";
$VAR{HASH}{NAME}[2]{ANOTHERHASH} = "test hash array hash 2";

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

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

发布评论

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

评论(3

ˇ宁静的妩媚 2024-10-04 07:43:45

我认为这没有任何理由行不通。您看到什么问题?

如果你想确保你的数据结构看起来像你所期望的那样,我推荐类似 Data::Dumper 的东西:

 # set up your %VAR hash as you like
 use Data::Dumper;
 print Dumper(\%VAR);

应该得到类似的东西:

$VAR1 = {
      'HASH' => {
                  'NAME' => [
                              'test hash array 1',
                              'test hash array 2',
                              {
                                'ANOTHERHASH' => 'test hash array hash 2',
                                'SOMEHASH' => 'test hash array hash 1'
                              }
                            ]
                },
      'ARRAY' => [
                   'test array'
                 ],
      'SCALAR' => 'test scalar'
    };

I see no reason why this wouldn't work. What issues are you seeing?

If you want to make sure your data structure looks like what you'd expect, I recommend something like Data::Dumper:

 # set up your %VAR hash as you like
 use Data::Dumper;
 print Dumper(\%VAR);

Should get something like:

$VAR1 = {
      'HASH' => {
                  'NAME' => [
                              'test hash array 1',
                              'test hash array 2',
                              {
                                'ANOTHERHASH' => 'test hash array hash 2',
                                'SOMEHASH' => 'test hash array hash 1'
                              }
                            ]
                },
      'ARRAY' => [
                   'test array'
                 ],
      'SCALAR' => 'test scalar'
    };
初心 2024-10-04 07:43:45

这不完全是你的问题,但是......如果你实际上以这种方式构建数据结构,你可能会考虑更干净的“文字”语法:

#!/usr/bin/perl
use strict;
use warnings;

my %VAR = (
  SCALAR => 'test scalar',
  ARRAY => [
    'test array',
  ],
  HASH => {
    NAME => [
      'test hash array 1',
      'test hash array 2',
      {
        SOMEHASH => 'test hash array hash 1',
        ANOTHERHASH => 'test hash array hash 2',
      },
    ],
  },
);

主要的两个原因是可读性和 autovivification 错误。这并不是 不正确的 perl,但它可能会导致难以调试的问题,例如意外键入:

$VAR{HASH}{NAME}[1] = "test hash array 1";
$VAR{HASH}{NAME}[2] = "test hash array 2";
$VAR{HASH}{NAME}[2] = "test hash array 3";
$VAR{HASH}{NAME}[4] = "test hash array 4";

而不是

$VAR{HASH}{NAME}[1] = "test hash array 1";
$VAR{HASH}{NAME}[2] = "test hash array 2";
$VAR{HASH}{NAME}[3] = "test hash array 3";
$VAR{HASH}{NAME}[4] = "test hash array 4";

Which can't be an issues if you're using

$VAR{HASH}{NAME} = [
  undef,
  'test hash array 1',
  'test hash array 2',
  'test hash array 3',
  'test hash array 4',
];

This isn't exactly your question, but... if you're actually building the data structure in that fashion, you might consider a cleaner "literal" syntax:

#!/usr/bin/perl
use strict;
use warnings;

my %VAR = (
  SCALAR => 'test scalar',
  ARRAY => [
    'test array',
  ],
  HASH => {
    NAME => [
      'test hash array 1',
      'test hash array 2',
      {
        SOMEHASH => 'test hash array hash 1',
        ANOTHERHASH => 'test hash array hash 2',
      },
    ],
  },
);

The main two reasons are readability and autovivification bugs. That's not incorrect perl, but it can lead to hard-to-debug issues, such as accidentally typing:

$VAR{HASH}{NAME}[1] = "test hash array 1";
$VAR{HASH}{NAME}[2] = "test hash array 2";
$VAR{HASH}{NAME}[2] = "test hash array 3";
$VAR{HASH}{NAME}[4] = "test hash array 4";

instead of

$VAR{HASH}{NAME}[1] = "test hash array 1";
$VAR{HASH}{NAME}[2] = "test hash array 2";
$VAR{HASH}{NAME}[3] = "test hash array 3";
$VAR{HASH}{NAME}[4] = "test hash array 4";

Which can't be an issue if you're using

$VAR{HASH}{NAME} = [
  undef,
  'test hash array 1',
  'test hash array 2',
  'test hash array 3',
  'test hash array 4',
];
小嗷兮 2024-10-04 07:43:45

通常,当人们抱怨价值观消失时,是因为他们取代了价值观。当您分配给散列的任何部分时,即使它是引用值,您也会替换之前存在的值:

use 5.010;

use Data::Dumper;

my %hash;

$hash{key} = { qw(a 1 b 2) };
say Dumper( \%hash );

$hash{key} = 5;
say Dumper( \%hash );

输出显示第二级的散列引用不再存在:

$VAR1 = {
          'key' => {
                     'a' => '1',
                     'b' => '2'
                   }
        };

$VAR1 = {
          'key' => 5
        };

您只需小心以及分配内容的位置,就像分配任何其他变量一样。

Often when people complain about disappearing values it's because they replaced them. When you assign to any part of a hash, you replace the value that was there previously even if it was a reference value:

use 5.010;

use Data::Dumper;

my %hash;

$hash{key} = { qw(a 1 b 2) };
say Dumper( \%hash );

$hash{key} = 5;
say Dumper( \%hash );

The output shows that the hash reference that was the second level is no longer there:

$VAR1 = {
          'key' => {
                     'a' => '1',
                     'b' => '2'
                   }
        };

$VAR1 = {
          'key' => 5
        };

You just have to be careful what and where you assign things, just like you do with any other variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文