Recursively printing data structures in Perl

前端 未结 8 1902
粉色の甜心
粉色の甜心 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:59

    Here is one simple example why your code is not easily readable:

    $delimiter = "\n--------------------------------------------" unless (defined $delimiter);
    

    You could use the defined or operator:

    $delimiter //= "\n" . '-' x 44;
    

    If you are worried about earlier Perls:

    defined $delimeter or $delimeter = "\n" . '-' x 44;
    

    Conditionals going off the right margin are enough of a turn-off for me not to read the rest of the code.

    0 讨论(0)
  • 2021-01-13 06:01
        #use strict ; 
        use warnings ; 
        # use module
        use XML::Simple;
        use Data::Dumper;
    
        #debug print "START SCRIPT " ; 
    
        my $fileToParse = 'C:/Temp/CDIP/scripts/perl/nps_all_workflows.xml' ; 
    
        # create object
        my $objXml= new XML::Simple;
    
        # read XML file
        my $data = $objXml->XMLin("$fileToParse");
    
        # #debug print "\n FirstLevel is " . $objXml->{'POWERMART'} ; 
        my $level = 1 ; 
    
        #
        printHashKeyValues ($data ) ;  
    
    
        sub printHashKeyValues  
        {
            $level ++ ; 
            my $refHash = shift ; 
            my $parentKey = shift ; 
            my $parentValue = shift ; 
    
    
            while( my ($key, $value) = each %$refHash) 
            {
    
    
                            if ( defined ( $key ) )
                            {
                                    if ( ref ($refHash->{"$key"}) eq 'HASH'  ) 
                                     {
                                     my $newRefHash = $refHash->{"$key"} ; 
                                     #debug print " \n The key is a hash " ; 
                                     printHashKeyValues ($newRefHash , $key , $value) ;
                                     }
    
    
                                        if ( ref ($refHash->{"$key"}) eq 'ARRAY'  ) 
                                         {
                                                #debug print " \n the key is an ARRAY " ; 
                                                printArrayValues ( $refHash->{"$key"} ) ; 
                                         }
    
    
                            } #eof  if ( defined ( $key ))
    
                            if ( defined ( $value) )
                            {
    
                                    if (  ref ($refHash->{"$value"}) eq 'HASH'  ) 
                                     {
                                     my $newRefHash = $refHash->{"$value"} ; 
                                    #debug print " \n The value is a hash " ; 
                                     printHashKeyValues ($newRefHash , $key , $value) ;
                                     }
    
    
                                    if ( ref ($refHash->{"$value"}) eq 'ARRAY'  ) 
                                     {
                                            #debug print " \n the value is an ARRAY " ; 
                                            printArrayValues ( $refHash->{"$value"} ) ; 
                                     }
    
                             } #eof if defined ( $value ) 
    
                                    #debug print "\n key: $key, value: $value.\n";
    
    
            } #eof while
        } #eof sub 
    
    
    
        sub printArrayValues
        {
            my $arrRef = shift ; 
            my @array = @$arrRef; 
            my $parrentArrayElement = shift ; 
    
                #debug print "printArrayValues CALLED " ;
    
            foreach my $arrayElement ( @array ) 
            {
                            if (defined ( $arrayElement ) )
                            {
                                    if   ( ref ($arrayElement) eq 'HASH'  ) 
                                 {
                                     #debug print " \n The  \$arrayElement is a hash FROM THE ARRAY " ; 
                                     printHashKeyValues ($arrayElement ) ;  
                                 } #eof if 
    
                                    if   ( ref ($arrayElement) eq 'ARRAY'  ) 
                                 {
                                     #debug print " \n The \$arrayElement is a ARRAY  FROM THE ARRAY " ; 
                                     printArrayValues ($arrayElement ) ;  
                                 } #eof if 
    
                                    #debug print "\n \$arrayElement is $arrayElement " ; 
                            } #eof if ( defined ( $arrayElement ) ) 
    
    
    
            } #eof foreach 
    
        } #eof sub 
    
    
    
    
        # #debug print output
        ##debug print Dumper($data);
    
    
    
    
        1 ; 
    
    0 讨论(0)
提交回复
热议问题