Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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".

In reply to Re: Challenge: Dumping trees. by brx
in thread Challenge: Dumping trees. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (8)
    As of 2015-07-07 05:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (87 votes), past polls