如何在Perl中创建哈希哈希数组,在Perl中打印它们并添加新的数组元素(即哈希的另一个哈希值)?

时间:2015-04-08 18:34:13

标签: arrays perl hash

下面应该如下所示,我想做以下

  1. 将哈希的新散列推入此数组
  2. 打印整个数组以及散列哈希值
  3. 能够读取子哈希元素的值

    my @AoH = (
      {
         undef
      },
      {
        Class1   => { hall_no => 10, Building_no => 1 },
        Subjects => { Math => 100, Physics => 200},
      },
      {
        Class2   => { hall_no => 10, Building_no => 2 },
        Subjects => { Chem => 100, Physics => 200},
      },
      {
        Class3   => { hall_no => 20, Building_no => 4 },
        Subjects => { Math => 100, Bio => 200},
      },    
    );
    
  4. 我试过这个

    1. 用于打印

      for $i ( 0 .. $#AoH ) {
        print "$i is { ";
        for $role ( keys %{ $AoH[$i] } ) {
          #print "$role=$AoH[$i]{$role} ";
          #DumperMsg("Results:",$AoH[$i]->"profile");
          print Dumper(%AoH[$i]);
        }
      
    2. 推送

      $AoH[3] = {Class4=> { hall_no => 20, Building_no => 1},};
      
    3. 没有运气,Perl的新手,谢谢。

2 个答案:

答案 0 :(得分:2)

  1. 将哈希的新散列推入此数组。

    push @array, \%hash_of_hashes;

  2. 打印整个数组以及散列散列

  3. 能够读取子哈希元素的值
  4. 这是两个相同的事情。阅读tutorial on Perl references。它将帮助解释如何使用引用来构建复杂的数据结构。

    #! /usr/bin/env perl
    #
    use strict;
    use warnings;
    use feature qw(say);
    
    my @array = ...;
    
    #
    # This is the outer array which is not a reference.
    #
    for my $index ( 0..$#array ) {
        #
        # Each element is a reference to the hashes of hashes. Dereference
        #
        my %hashes_of_hashes = %{ $array[$index] };
        #
        # This is going through each element of the outer hash.
        # Each item is a key to a REFERENCE to another hash
        #
        for my $sub_hash_key ( sort keys %hashes_of_hashes ) {
            #
            # Dereference that inner hash
            #
            my %sub_hash = %{ $hashes_of_hashes{$sub_hash_key} };
            #
            # Finally, We have the inner hash. Each element contains
            # data and not another reference
            #
            for my $sub_key ( sort keys %sub_hash ) {
                #
                # This is how I refer to an item in that array of
                # Hashes of Hashes
                #
                print "\$array[$index]->{$sub_hash_key}->{$sub_key} = ";
                #
                # Since I dereferenced everything along the way, I can
                # easily print out the value via the sub_hash.
                #
                say $sub_hash{$sub_key};
            }
        }
    }
    

    最大的诀窍是保持你的结构平直。这是哈希引用或数据的关键吗?这是一个数组还是哈希?取消引用您的引用可以使程序更容易遵循。我没有必要进行常量的解除引用,但你可以想象内部的for循环很难用所有引用的引用来阅读。

    有三种语法可以在引用中获取值:

    1. ${${$array[$index]}{$sub_hash_key}}{$sub_key};
    2. $array[1]{$hofh_key}{$hash_key}
    3. $array[1]->{$hofh_key}->{$hash_key}
    4. 第一种方法是使用解除引用来生成正确的值。它很快就会变得很困难。

      第二个是快捷方式,它允许Perl采用默认的解除引用。

      第三个使用箭头语法指向到引用。我喜欢这种方式,因为它很清楚我在谈论一个参考。你不能轻易掩饰它。

      您有时也可以通过简单地省略花括号来取消引用。这些是相同的:

      • my %hash = %$hash_ref;
      • my %hash = %{ $hash_ref };

      同样,我喜欢第二种方式,因为它很容易错过 array 前面的双重印记。许多人更喜欢第一种,因为他们认为语法更清晰,更容易阅读。

      如果您的数据结构开始变得复杂,那么现在是时候考虑Object Oriented Perl

答案 1 :(得分:0)

这是我的看法。但我的建议是坐下来半天,并在learn.perl.org找到一些教程。

#!/usr/bin/env perl 

use strict;
use warnings;

use Data::Dumper;

# set up initial array of hashes
my @lessons = (
  {
    class_room => { hall_no => 10, building_no => 1 },
    subjects   => { Math => 100, Physics => 200},
  },
  {
    class_room => { hall_no => 10, building_no => 2 },
    subjects   => { Chem => 100, Physics => 200},
  },
);

# Push new hash of hashes into this array
push @lessons,      
      {
        class_room   => { hall_no => 20, building_no => 4 },
        subjects     => { Math => 100, Bio => 200},
      };

# Print the entire array along with hash of hashes
print Dumper( \@lessons );

# Able to read off values of the sub hash elements
foreach my $lesson ( @lessons ) {
    print "In building $lesson->{class_room}->{building_no} hall $lesson->{class_room}->{hall_no} we have the following classes:\n";
    while ( my ($subject, $level) = each %{$lesson->{subjects}} ) {
        print "$subject $level\n";
    }
    print "\n";
}