2

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?

himmat
  • 159
  • 5

1 Answers1

2

$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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Sorry for being naive. In PERL 5.8.1 I get compilation error "Can't call method "say" on unblessed reference at \script... " When I put his statement "say $register->{register_name};" in code. may i replace say with print statement? – himmat Mar 19 '20 at 16:40
  • Always use `use strict; use warnings;`, and if a appropriate, use `use feature qw( say );` or equivalent. I don't bother putting that into each code snippet since it would clutter everything up, especially since the solution is made obvious by the docs for [`say`](http://perldoc.perl.org/functions/say.html). – ikegami Mar 19 '20 at 16:48
  • And again, the name is "Perl", not "PERL". (Why are you using a version from 20 years ago!?) – ikegami Mar 19 '20 at 16:51
  • Yay, I made it work using the following code. $local_variable=$register->{register_name}; print "$local_variable"; – himmat Mar 19 '20 at 17:02
  • It should be `print "$local_variable\n";`, but `say $local_variable;` is shorter and cleaner. – ikegami Mar 19 '20 at 17:10
  • Perl-5.8.8, My organization has randomly spread modules in different Perl versions. yes, I have already added use strict; use warnings in the code. – himmat Mar 19 '20 at 18:53
  • eh? What happened to my comment saying that `say` won't work with 5.8. (It requires 5.10) – ikegami Mar 20 '20 at 04:12