我如何访问内在属性?

发布于 2024-10-14 20:36:41 字数 480 浏览 2 评论 0原文

我有一个像这样的 perl 变量。 我如何访问内置属性(如“706”)?

my @config = [
        {
        'x' => [ 565, 706 ],
        'y' => [ 122 ],
        'z' => 34,
        'za' => 59,
    }
];

编辑: print Dumper($config[0]); 产量:< code>$VAR1 = undef;

看起来我使用 $config[0][0]->{x}[1]; 获得访问权限。为什么我必须使用 [0][0] (一个很清楚,但他第二个......)?

EDIT2:我很抱歉更改了数据结构,但是给我的定义发生了变化。

I have a perl variable like this. How can i access the inlying properties (like '706')?

my @config = [
        {
        'x' => [ 565, 706 ],
        'y' => [ 122 ],
        'z' => 34,
        'za' => 59,
    }
];

EDIT: print Dumper($config[0]); yields : $VAR1 = undef;

Looks like i get acces using $config[0][0]->{x}[1];. Why do i have to use [0][0] (one is clear, but he ssecond...)?

EDIT2: I am sorry for changing the data structure, but the definition which was given to me changed.

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

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

发布评论

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

评论(2

半透明的墙 2024-10-21 20:36:41

您的变量相当于:

my $config = [
    'x', [ 565, 706 ],
    'y', [ 122 ],
    'z', 34,
    'za', 59,
];

因此,如果您想获得 706,您可以执行以下操作:

print $config->[1][1];

根据问题中的新数据更新

有了更新的问题,您现在可以通过以下方式访问:

say $config->[0]{x}[1];

新更新根据新的数据结构

根据您提供的最后更新的数据结构:

my @config = [
        {
        'x' => [ 565, 706 ],
        'y' => [ 122 ],
        'z' => 34,
        'za' => 59,
    }
];

您分配一个匿名数组[...],它本身包含一个哈希{...}
到数组@config,这将填充@config的第一个元素
对于匿名数组,

say Dumper \@config;

$VAR1 = [
          [
            {
              'y' => [
                       122
                     ],
              'za' => 59,
              'x' => [
                       565,
                       706
                     ],
              'z' => 34
            }
          ]
        ];
say $config[0][0]{x}[1];  #prints 706

我认为您想要执行以下任一操作:

my $config = [
        {
        'x' => [ 565, 706 ],
        'y' => [ 122 ],
        'z' => 34,
        'za' => 59,
    }
];
say $config->[0]{x}[1]; #prints 706



my @config = (
        {
        'x' => [ 565, 706 ],
        'y' => [ 122 ],
        'z' => 34,
        'za' => 59,
    }
);
say $config[0]{x}[1];  #prints 706

Your variable is equivalent to :

my $config = [
    'x', [ 565, 706 ],
    'y', [ 122 ],
    'z', 34,
    'za', 59,
];

So if you want to get the 706, you can do:

print $config->[1][1];

Updated according to new data in the question

With the updated question, you can access now by :

say $config->[0]{x}[1];

New update according to new data structure

According to the last updated data structure you provide:

my @config = [
        {
        'x' => [ 565, 706 ],
        'y' => [ 122 ],
        'z' => 34,
        'za' => 59,
    }
];

you assign an anonymous array [...] that contains itself a hash {...}
to an array @config, this will populate the first element of @config
with the anonymous array

say Dumper \@config;

$VAR1 = [
          [
            {
              'y' => [
                       122
                     ],
              'za' => 59,
              'x' => [
                       565,
                       706
                     ],
              'z' => 34
            }
          ]
        ];
say $config[0][0]{x}[1];  #prints 706

I think you want to do either:

my $config = [
        {
        'x' => [ 565, 706 ],
        'y' => [ 122 ],
        'z' => 34,
        'za' => 59,
    }
];
say $config->[0]{x}[1]; #prints 706



my @config = (
        {
        'x' => [ 565, 706 ],
        'y' => [ 122 ],
        'z' => 34,
        'za' => 59,
    }
);
say $config[0]{x}[1];  #prints 706
三五鸿雁 2024-10-21 20:36:41

[编辑: 遵循转移问题定义。]

给出:

my @config = ( 
  [
    { # NB: insertion order ≠ traversal order
        "x"  => [ 565, 706 ],
        "y"  => [ 122 ],
        "z"  => 34,
        "za" => 59,
    },
  ],
);

那么这就可以了:

# choice §1
print $config[0][0]{"x"}[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

当然理解这只是语法糖:

# choice §2
print $config[0]->[0]->{"x"}->[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

并且这只是语法糖for:

# choice §3
print ${ $config[0] }[0]->{"x"}->[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

反过来又只是语法糖 for:

# choice §4
print ${ ${ $config[0] }[0] }{"x"}->[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

又是语法糖 for:

# choice §5
print ${ ${ ${ $config[0] }[0] }{"x"}}[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

当然,它等价于:

# choice §6
print ${ ${ ${ $config[0] }[0] }{"x"} }[ $#{ ${ ${ $config[0] }[0] }{"x"} } ];   # get 1ˢᵗ row’s xᵗʰ row’s last element

[EDIT: Follow the shifting problem definition.]

Given:

my @config = ( 
  [
    { # NB: insertion order ≠ traversal order
        "x"  => [ 565, 706 ],
        "y"  => [ 122 ],
        "z"  => 34,
        "za" => 59,
    },
  ],
);

Then this will do it:

# choice §1
print $config[0][0]{"x"}[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

understanding of course that that is merely syntactic sugar for:

# choice §2
print $config[0]->[0]->{"x"}->[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

and that that is just syntactic sugar for:

# choice §3
print ${ $config[0] }[0]->{"x"}->[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

which in turn is just syntactic sugar for:

# choice §4
print ${ ${ $config[0] }[0] }{"x"}->[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

which again is syntactic sugar for:

# choice §5
print ${ ${ ${ $config[0] }[0] }{"x"}}[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element

and that, of course, is equivalent to:

# choice §6
print ${ ${ ${ $config[0] }[0] }{"x"} }[ $#{ ${ ${ $config[0] }[0] }{"x"} } ];   # get 1ˢᵗ row’s xᵗʰ row’s last element
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文