How to trace back the original Data structure information from the PERL dump?

Viewed 84

I am working on parsing the few .xlsx sheets to create a new sheet with there conditional data usage. I came through the following data structure and didn't understand if its array of hashes or hashes of hashes. I dumped the following data structure using dump command.

print Dumper $hash_reg_list{first_type}{registers};

            [
          {
            'fields' => [
                          {
                            'field_name' => ' IP_NAME_AACTV_SFE'
                          },
                          {
                            'field_name' => ' IP_NAME_AACTV_PP'
                          },

                        ],
            'register_name' => ' IP_NAME_AACTV'
          },
          {
            'fields' => [
                          {
                            'field_name' => ' IP_NAME_CONFIG_LO_KHD_L2DEMOTE'
                          }
                        ],
            'register_name' => ' IP_NAME_CONFIG'
          },
          {
            'fields' => [
                          {
                            'field_name' => ' IP_NAME_JIK_FE_MIN_TST'
                          },
                                                    {
                            'field_name' => ' IP_NAME_JIK_TM_LOOPA_HYU'
                          }
                        ],
            'register_name' => ' IP_NAME_JIK'
          },

]

I want to print all register_name from the above data structure.

my @sorted_mm_register_list_1;
foreach my $register_name (sort keys %hash_reg_list{first_type}{registers})
    {
       $sorted_mm_register_list_1[$list_index] = $register_name;
       $list_index++;
       print "$hash_reg_list{first_type}{registers}{register_name}";
       printf("listindex: %s\n", $list_index);

    }

How shall I trace back the following data structure dumped from the PERL? Any help would be appreciated?

1 Answers

$hash_reg_list{first_type}{registers} is a reference to an array. You want to iterate over the elements of the referenced array.

for my $register (@{ $hash_reg_list{first_type}{registers} }) {
   ...
}

or

for my $register ($hash_reg_list{first_type}{registers}->@*) {
   ...
}

(->@* requires Perl 5.24, or Perl 5.20 and use experimental qw( postderef );)


$register is the value of the current element of the array, which is a reference to a hash. You want to print the element of that hash with the key register_name.

say $register->{register_name};

All together, we get

for my $register (@{ $hash_reg_list{first_type}{registers} }) {
   say $register->{register_name};
}

or

for my $register ($hash_reg_list{first_type}{registers}->@*) {
   say $register->{register_name};
}

If we wanted to sort the registers by register name, we would use the following:

for my $register (
   sort { $a->{register_name} cmp $b->{register_name} }
      @{ $hash_reg_list{first_type}{registers} }
) {
   say $register->{register_name};
}

or

for my $register (
   sort { $a->{register_name} cmp $b->{register_name} }
      $hash_reg_list{first_type}{registers}->@*
) {
   say $register->{register_name};
}

See Perl Dereferencing Syntax for information on dereferencing and links to further information.

Related