perltidy



use strict;
use Data::Dumper;

sub printAsArray($$) {
	my $theHashArray = shift;
	my $theRef       = shift;

	my @theKeys   = ();
	my $theRef_is = ref($theRef);
	if ( $theRef_is eq 'HASH' ) {
		die( "The provided hashRef must contain a set of keys:\n", Dumper( $theRef, ), "\n", ) if ( !exists $theRef->{keys} );
		push( @theKeys, @{ $theRef->{keys} }, );
	}
	elsif ( $theRef_is eq 'ARRAY' ) {
		push( @theKeys, @{$theRef}, );
	}
	else {
		die( "\$theRef_is = '$theRef_is';\n", Dumper( $theRef, ), "\n", );
	}

	my @outputLines = ();
	foreach my $hashRef ( @{$theHashArray} ) {
		my @aLine = ();
		foreach my $key (@theKeys) {
			$hashRef->{$key} = '' if ( !exists $hashRef->{$key} );
			push( @aLine, $hashRef->{$key}, );
		}
		push( @outputLines, \@aLine, );
	}

	my @lengths   = ();
	my $preFormat = '|';
	foreach my $key (@theKeys) {
		my $ll = length($key);
		push( @lengths, $ll, );
		if ( $theRef_is eq 'HASH' && exists $theRef->{justify}->{$key} ) {
			if ( $theRef->{justify}->{$key} eq 'left' ) {
				$preFormat .= ' %%-%ds |';
			}
			else {
				$preFormat .= ' %%%ds |';
			}
		}
		else {
			$preFormat .= ' %%-%ds |';
		}
	}
	$preFormat .= "\n";

	foreach my $line (@outputLines) {
		foreach my $ii ( 0 .. $#{$line} ) {
			my $ll = length( $line->[$ii] );
			$lengths[$ii] = ( $ll > $lengths[$ii] ) ? $ll : $lengths[$ii];
		}
	}
	my $theFormat = sprintf( $preFormat, @lengths, );

	my $breakLine = '+';
	foreach my $ll (@lengths) {
		$breakLine .= '-';
		$breakLine .= '-' x $ll;
		$breakLine .= '-+';
	}
	$breakLine .= "\n";

	print( $breakLine, );
	printf( $theFormat, @theKeys, );
	print( $breakLine, );
	foreach my $line (@outputLines) {
		printf( $theFormat, @{$line}, );
	}
	print( $breakLine, );
}

1;