I know it's a little bit late...
use strict;
my $sp = 1;
my $tree = [
[
[[[[["alpha", "bravo"], "charlie"], ["delta", "echo"]], [[["fo
+xtrot", "golf"], "hotel"], [["indigo", "juliet"], ["kilo", ["lima", "
+mike"]]]]],
["november", [[["oscar", "papa"], "quebec"], ["romeo", "sierra
+"]]]], ["tango", ["uniform", "victor"]]
],
[["whiskey", ["xray", "yankee"]], "zulu"]
];
my $lowerline = 0;
my $nodecount =0;
sub scantree {
my ($node,$level) = @_;
if (ref($node)) {
$level++;
scantree($_,$level) for (@$node);
} else {
$nodecount++;
my $ground = $level + length($node);
$lowerline = $ground if ($ground > $lowerline);
}
}
scantree($tree,0); # get number of columns and lines ($nodecount(* $sp
+) and $lowerline)
my @output = (" " x ((1+$sp)*$nodecount)) x ($lowerline+1);
$nodecount = 0; #reset
sub show {
my ($node,$level,$rl) = @_;
if (ref($node)) {
my $left=0; #0 is right // 1 is left
my @po;
for my $n (@$node) {
$po[$left] = show($n,$level+1,$left);
$left++;
}
# ____
# /size\
my $size = $po[1]-$po[0]-1;
substr($output[$level] , $po[0]+1, $size, "_" x $size);
substr($output[$level+1], $po[0] , 1 , "/");
substr($output[$level+1], $po[1] , 1 , "\\");
return($po[1-$rl]); #child return "hook" position to parent
} else {
$nodecount++;
my $p = ($nodecount-1)*($sp+1);
for my $l (1 .. length($node)) {
substr($output[$level+$l], $p+$sp, 1, substr($node,$l-1,1)
+); #write $node vertically
}
return ($p+$sp); # child returns his position to parent
}
}
show($tree,0,0);
my $out = join "\n",@output;
print "$out\n";
__END__
#lowcost new view :
my $col = length (($out=~/.(.*)/)[0]);
my $numlines = $out=~ tr/\n//;
for my $x (0 .. $col) {
for my $y (0 .. $numlines) {
my $ch = substr($out,$x+$y*($col+2),1);
$ch =~ tr/_/|/;
print $ch;
}
print "\n";
}
__DATA__
$sp=1
_______
___________/ \___
___________/ \_ _/ \
_______/ \_____ / \_ / \_ z
_/ \___ / \_ t / \ w / \ u
_/ \_ _/ \_ n _/ \_ a u v h x y l
_/ \ / \ _/ \ _/ \_ o _/ \ / \ n n i i r a u
/ \ c d e / \ h / \ / \_ v / \ q r s g i c s a n
a b h e c f g o i j k / \ e o p u o i o f t k y k
l r a l h o o t n u i l m m s a e m e o o e e
p a r t o x l e d l l i i b c p b e r r r y e
h v l a t f l i i o m k e a a e o r m
a o i r g e a e r r c a
e o o t
t
-----
$sp=2
__________
+_
_________________/
+ \_____
_________________/ \__ _
+_/ \
___________/ \________ / \__ /
+ \__ z
__/ \_____ / \__ t / \ w
+ / \ u
__/ \__ __/ \__ n __/ \__ a u v h
+ x y l
__/ \ / \ __/ \ __/ \__ o __/ \ / \ n n i i
+ r a u
/ \ c d e / \ h / \ / \__ v / \ q r s g i c s
+ a n
a b h e c f g o i j k / \ e o p u o i o f t k
+ y k
l r a l h o o t n u i l m m s a e m e o o e
+ e
p a r t o x l e d l l i i b c p b e r r r y
+ e
h v l a t f l i i o m k e a a e o r m
a o i r g e a e r r c a
e o o t
t
-----
$sp=3
+ _______________
______________
+_________/ \_______
_______________________/
+ \___ ___/ \
_______________/ \___________
+ / \___ / \___ z
___/ \_______ / \__
+_ t / \ w / \ u
___/ \___ ___/ \___ n ___/
+ \___ a u v h x y l
___/ \ / \ ___/ \ ___/ \___ o ___/ \
+ / \ n n i i r a u
/ \ c d e / \ h / \ / \___ v / \ q
+ r s g i c s a n
a b h e c f g o i j k / \ e o p u
+ o i o f t k y k
l r a l h o o t n u i l m m s a e
+ m e o o e e
p a r t o x l e d l l i i b c p b
+ e r r r y e
h v l a t f l i i o m k e a a e
+ o r m
a o i r g e a e r r c
+ a
e o o t
t
-----
$sp=0
___
_____/ \_
_____/ \ / \
___/ \__ /\ /\ z
/ \_ / \ t/\w/\u
/\ / \ n /\ auvhxyl
/\/\ /\ /\ o /\/\nniirau
/\cde/\h/\/\ v/\qrsgicsan
abhecfgoijk/\eopuoioftkyk
lralhootnuilmmsaeme ooe e
partoxledlliibcpber rry e
hvla tfliiomkeaaeor m
aoi r ge aerr c a
e o ot
t
----
lowcost new view ($sp=1)
/alpha
|
/\bravo
|
/\charlie
|
/\/delta
| |
| \echo
|
| /foxtrot
| |
| /\golf
| |
/\/\hotel
| |
| | /indigo
| | |
| \/\juliet
| |
| \/kilo
| |
| \/lima
| |
| \mike
|
/\/november
| |
| | /oscar
| | |
| | /\papa
| | |
| \/\quebec
| |
| \/romeo
| |
| \sierra
|
/\/tango
| |
| \/uniform
| |
| \victor
|
| /whiskey
| |
\/\/xray
| |
| \yankee
|
\zulu
English is not my mother tongue.
Les tongues de ma mère sont "made in France".
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.