Perl关于数据结构的查询

发布于 2024-12-23 15:05:23 字数 406 浏览 2 评论 0原文

我目前使用以下哈希,效果很好

%hash = ( 
    'env1' => 'server1:port1, server11:port11',
    'env2' => 'server2:port2, server22:port22'
) ;

但是,我真正想做的是创建以下数据结构,这将使我更容易提取信息。下面的显然行不通。

(
  env1 => "server=server1, port=port1", "server=server11, port=port11",
  env2 => "server=server2, port=port2", "server=server22, port=port22"
) ;

想知道是否有人对创建符合我的要求的数据结构有任何建议。

I currently use the following hash which works fine

%hash = ( 
    'env1' => 'server1:port1, server11:port11',
    'env2' => 'server2:port2, server22:port22'
) ;

However, what I really want to do is create the following data structure, which will make it easier for me to extract the information. The following obviously does not work.

(
  env1 => "server=server1, port=port1", "server=server11, port=port11",
  env2 => "server=server2, port=port2", "server=server22, port=port22"
) ;

Wondering if anyone has any suggestions on creating a data structure that would match my requirements.

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

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

发布评论

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

评论(2

淡紫姑娘! 2024-12-30 15:05:23

写这样:

%hash = (
  env1 => ["server=server1, port=port1", "server=server11, port=port11"],
  env2 => ["server=server2, port=port2", "server=server22, port=port22"]
) ;

然后像这样访问元素:

$hash{'env1'}->[0] == "server=server1, port=port1"
$hash{'env2'}->[1] == "server=server22, port=port22"

这是一个散列,其中值是对匿名数组的引用。

但是当我查看您的数据时,我认为也许有更好的方法来存储它:

%hash = (
  env1 => [{'server' => 'server1', 'port' => 'port1'}, {'server' => 'server11', 'port' => 'port11'}],
  env2 => [{'server' => 'server2', 'port' => 'port2'}, {'server' => 'server22', 'port' => 'port22'}]
) ;

然后访问如下元素:

$hash{'env1'}->[0]->{'server'} == "server1"
$hash{'env2'}->[1]->{'port'} == "port22"

Write this:

%hash = (
  env1 => ["server=server1, port=port1", "server=server11, port=port11"],
  env2 => ["server=server2, port=port2", "server=server22, port=port22"]
) ;

And then access elements like this:

$hash{'env1'}->[0] == "server=server1, port=port1"
$hash{'env2'}->[1] == "server=server22, port=port22"

This is a hash where the values are references to anonymous arrays.

But when I look at you data I think maybe there is a better way to store it:

%hash = (
  env1 => [{'server' => 'server1', 'port' => 'port1'}, {'server' => 'server11', 'port' => 'port11'}],
  env2 => [{'server' => 'server2', 'port' => 'port2'}, {'server' => 'server22', 'port' => 'port22'}]
) ;

And then access elements like this:

$hash{'env1'}->[0]->{'server'} == "server1"
$hash{'env2'}->[1]->{'port'} == "port22"
凌乱心跳 2024-12-30 15:05:23

鉴于这个问题,很难准确说出您在寻找什么。我怀疑哈希值的哈希值就足够了。您可以这样设置:

%hash = ( 'env1' => { 'server' => 'server1', 'port' => 'port1' }, 
          'env2' => { 'server' => 'server2', 'port' => 'port2' } );

要获取值,您可以执行以下操作:

print $hash{'env2'}->{'server'};

您可以添加其他值,如下所示:

$hash{'env3'} = {'server' => 'server3', 'port' => 'port3'};

Hard to tell exactly what you're looking for given the question. I suspect a hash of hashes will suffice. You'd set it up like this:

%hash = ( 'env1' => { 'server' => 'server1', 'port' => 'port1' }, 
          'env2' => { 'server' => 'server2', 'port' => 'port2' } );

To get the values, you'd do something like this:

print $hash{'env2'}->{'server'};

You can add additional values like this:

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