Traversing a multi-dimensional hash in Perl

后端 未结 8 1971
后悔当初
后悔当初 2021-01-03 04:02

If you have a hash (or reference to a hash) in perl with many dimensions and you want to iterate across all values, what\'s the best way to do it. In other words, if we hav

8条回答
  •  感动是毒
    2021-01-03 04:35

    Keep in mind that Perl lists and hashes do not have dimensions and so cannot be multidimensional. What you can have is a hash item that is set to reference another hash or list. This can be used to create fake multidimensional structures.

    Once you realize this, things become easy. For example:

    sub f($) {
      my $x = shift;
      if( ref $x eq 'HASH' ) {
        foreach( values %$x ) {
          f($_);
        }
      } elsif( ref $x eq 'ARRAY' ) {
        foreach( @$x ) {
          f($_);
        }
      }
    }
    

    Add whatever else needs to be done besides traversing the structure, of course.

    One nifty way to do what you need is to pass a code reference to be called from inside f. By using sub prototyping you could even make the calls look like Perl's grep and map functions.

提交回复
热议问题