### $Id$ ### Author: Dr.A.Davis & G.Hamell ### NAME: Hash_Print.pm ### Copyright (c) 2005, California Institute of Technology. All rights ### reserved. Unauthorized reproduction prohibited. ### ### ### CALLING SEQUENCE: Hash_Print::hash_print.pm ### ### PURPOSE: ### Print an entire Perl hash, traversing down to lowest levels. ### ### INPUT: ### A single reference arg is expected by print_hash. ### ### OUTPUT: ### Keys & values (& in case of arrays, sub values) ### ### RETURN: ### ### ### MODIFICATION HISTORY: ### ===================== ### 2005Mar11-Glenn Hamell Created. ### ################################################### package Hash_Print; # use strict; ### ===== start hash_print ===== sub hash_print { # Two args: hash ref, depth(indentation) counter my ($href, $depth) = @_; while( my ( $k, $v ) = each %$href ) { if (ref $v eq 'HASH') { print_tabs( $depth++ ); print "$k\n"; hash_print( $v, $depth, $k ); $depth--; } elsif (ref $v eq 'ARRAY') { print_tabs( $depth++ ); print "$k\n"; array_print( $v, $depth ) ; $depth--; } else { print_tabs( $depth++ ); printf "%-20s\t%s\n",$k,$v; $depth--; } } } # - - - end hash_print - - - ### ===== start array_print ===== sub array_print { # Two args: hash ref & depth(indentation) counter my ($href, $depth) = @_; my $nelem = $#$href; # last index for( my $i=0; $i <= $nelem; $i++ ) { if ($depth==1) {print "\n*************************************************\n";} my $v = $$href[$i] ; if( ref $v eq 'HASH' ) { hash_print( $v, $depth) ; } elsif ( ref $v eq 'ARRAY' ) { array_print( $v, $depth ); } else { print_tabs( $depth++ ); # printf "\t\t%-20s\t%s\n",$v; print "\t\t$v\n"; $depth--; } } } # - - - end array_print - - - ### ===== start print_tabs ===== sub print_tabs { # arg: depth(indentation) counter my $tabs = $_[0]; while( $tabs ) { print "\t"; $tabs--; } } # - - - end print_tabs - - - 1; ###------------------------------------------------