#!/usr/bin/perl # http://www.rosettacode.org/wiki/Display_an_outline_as_a_nested_table use strict; use warnings; my @rows; my $row = -1; my $width = 0; my $color = 0; our $bg = 'e0ffe0'; parseoutline( do { local $/; =~ s/\t/ /gr } ); print "\n"; for ( @rows ) { my $start = 0; print " \n"; for ( @$_ ) # columns { my ($data, $col, $span, $bg) = @$_; print " \n" x ( $col - $start ), " \n"; $start = $col + $span; } print " \n" x ( $width - $start ), " \n"; } print "
$data
\n"; sub parseoutline { ++$row; while( $_[0] =~ /^( *)(.*)\n((?:\1 .*\n)*)/gm ) { my ($head, $body, $col) = ($2, $3, $width); $row == 1 and local $bg = qw( ffffe0 ffe0e0 )[ $color ^= 1]; if( length $body ) { parseoutline( $body ) } else { ++$width } push @{ $rows[$row] }, [ $head, $col, $width - $col, $bg ]; } --$row; } __DATA__ insects large wings butterfly small or no wings very long rear legs antenna front mosquito antenna rear grasshopper shorter rear legs horned head rhino beetle not horned head small eyes termite solder large eyes beetle #### +------------------------------------------------------------------------+ | insects | |------------------------------------------------------------------------| | large wings | small or no wings | |-------------+----------------------------------------------------------| | butterfly | very long rear legs | shorter rear legs | |-------------+--------------------------+-------------------------------| | | antenna | antenna rear | horned | not horned head | | | front | | head | | |-------------+-----------+--------------+---------+---------------------| | | mosquito | grasshopper | rhino | small | large | | | | | beetle | eyes | eyes | |-------------+-----------+--------------+---------+-----------+---------| | | | | | termite | beetle | | | | | | solder | | +------------------------------------------------------------------------+