简体   繁体   English

如何遍历perl中的哈希哈希并访问包含的数据?

[英]How do I loop over a hash of hashes in perl and access the data contained?

I have a list 我有一个清单

my %SERVICES = (
              name=>
              {
                 description => 'Item 1',
                 service_codes => [ 'item1' ],
              },
              name2=>
              {
                 description => 'Item 2',
                 service_codes => [ 'item2' ],
              },
            );

what I need to do is reference the description and print it to a variable called $service_name so when referenced the text will read Item 1 or Item 2. 我需要做的是参考描述并将其打印到一个名为$ service_name的变量中,以便在引用该文本时将显示项目1或项目2。

I am VERY new at perl and am trying to learn this on my own the best as I can. 我在perl上非常陌生,并且正在努力尽我所能学习这一点。 Any help would be appreciated, please use small words. 任何帮助将不胜感激,请用小写字母。 :) :)

Easy enough, try looping over the data structure 足够简单,尝试遍历数据结构

while ( my ($k, $data) = each %SERVICES ) {
  my $service_name = $data->{description};
  say $service_name;
}

Program: 程序:

foreach my $id (keys %SERVICES)
{
  my $des = $SERVICES{$id}{'description'};
  print "$des\n";
}

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

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