Recursively printing data structures in Perl

前端 未结 8 1901
粉色の甜心
粉色の甜心 2021-01-13 05:32

I am currently learning Perl. I have Perl hash that contains references to hashes and arrays. The hashes and arrays may in turn contain references to other hashes/arrays.

相关标签:
8条回答
  • 2021-01-13 05:39
    1. Always use use strict;
    2. To be a good boy, use use warnings as well.
    3. The names you use for subroutines should make it obvious what the subroutine does. "recurseErrors" kind of violates that principle. Yes, it does recurse. But what errors?
    4. On the first line of each subroutine you should declare and initialize any parameters. recurseErrors first declares $str and then declares its parameters.
    5. Don't mix shift and = @_ like you do in str()
    6. You might consider breaking up what is now called recurseErrors into specialized routines for handling arrays and hashes.
    7. There's no need to quote variables like you do on lines 99 and 109.

    Apart from that I think your instructor had a bad day that day.

    0 讨论(0)
  • 2021-01-13 05:45

    maybe Data::Dumper is what you want:

    use Data::Dumper;
    
    $str = Dumper($foo);
    print($str);
    
    0 讨论(0)
  • 2021-01-13 05:45

    I've struggled with this same problem before, and found my way here. I almost used a solution posted here, but found a more suitable one (for me anyway). Read about Depth First Recursion here.

    The sub in the above article works perfectly with a reference containing other Hashes, Arrays, or Scalars. It did not print Hash key names, though, so I slightly modified it:

    #!/usr/bin/perl
    #
    # See:
    #
    # http://perldesignpatterns.com/?DepthFirstRecursion
    #
    use strict;
    use warnings;
    
    my %hash = (
      'a' => {
        'one' => 1111,
        'two' => 222,
      },
      'b' => [ 'foo', 'bar' ],
      'c' => 'test',
      'd' => {
        'states' => {
          'virginia' => 'richmond',
          'texas' => 'austin',
        },
        'planets' => [ 'venus','earth','mars' ],
        'constellations' => ['orion','ursa major' ],
        'galaxies' => {
          'milky way' => 'barred spiral',
          'm87' => 'elliptical',
        },
      },
    );
    
    &expand_references2(\%hash);
    
    sub expand_references2 {
      my $indenting = -1;
      my $inner; $inner = sub {
        my $ref = $_[0];
        my $key = $_[1];
        $indenting++;
        if(ref $ref eq 'ARRAY'){
          print '  ' x $indenting,'ARRAY:';
          printf("%s\n",($key) ? $key : '');
          $inner->($_) for @{$ref};
        }elsif(ref $ref eq 'HASH'){
          print '  ' x $indenting,'HASH:';
          printf("%s\n",($key) ? $key : '');
          for my $k(sort keys %{$ref}){
            $inner->($ref->{$k},$k);
          }
        }else{
          if($key){
            print '  ' x $indenting,$key,' => ',$ref,"\n";
          }else{
            print '  ' x $indenting,$ref,"\n";
          }
        }
        $indenting--;
      };
      $inner->($_) for @_;
    }
    
    0 讨论(0)
  • 2021-01-13 05:49

    If you are new to perl, I'd recommend running your code through perl-critic (there is also a script you can install from CPAN, normally I use it as a test so it gets run from the command line whenever I do "make test"). In addition to its output, you might want to break up your functions a bit more. recurseErrors has three cases that could be split into sub functions (or even put into a hash of ref-type to sub-function ref).

    If this were a production job, I'd use Data::Dumper, but it sounds like this is homework, so your teacher might not be too pleased.

    0 讨论(0)
  • 2021-01-13 05:51

    You could have separated out the code blocks that dealt with arrays, and hashes.

    sub recurse{
      ...
      recurse_A(@_) if $ref eq 'ARRAY';
      recurse_H(@_) if $ref eq 'HASH';
      ...
    }
    
    sub recurse_A{ ... }
    sub recurse_H{ ... }
    

    I would recommend starting out your subroutines like this, unless you have a real good reason for doing otherwise.

    sub example{
      my( $one, $two, $three, $optional_four ) = @_;
    

    ( If you do it like this then Komodo, at least, will be able to figure out what the arguments are to your subroutine )

    There is rarely any reason to put a variable into a string containing only the variable.

    "$var" eq $var;
    

    The only time I can think I would ever do that is when I am using an object that has an overloaded "" function, and I want to get the string, without also getting the object.

    package My_Class;
    use overload
      '""' => 'Stringify',
    ;
    sub new{
      my( $class, $name ) = @_;
      my $self = bless { name => $name }, $class;
      return $self;
    }
    sub Stringify{
      my( $self ) = @_;
      return $self->{name};
    }
    

    my $object = My_Class->new;
    my $string = "$object";
    
    0 讨论(0)
  • 2021-01-13 05:53

    My guess is that he doesn't like that you

    1. expect a hash in the str function.
    2. call the same function to print arrays as hashes, despite that there appears to be no common function between them.
    3. allow various ways to call str, but it never figures into the final result.
    4. allow configurable space to be passed in to the root function, but have a tab hardcoded in the recursive function.
    5. omit undefined values that actually hold a place in the arrays

    Those are issues that I can see, pretty quickly.

    0 讨论(0)
提交回复
热议问题