Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
As a side note @RosettaStone: Many languages just showed their Data::Dumper equivalent! :-|

my emphasize is on readability and flexibility for alternative formats, but YMMV! =)

#!/usr/bin/perl use warnings; use strict; use Data::Dump qw/pp/; #use diagnostics; use Test::More tests => 1; my $input = <<'__IN__'; A | |--B | | | |--C | | | |---PQR | |---XYZ |--D | | | |---LMN |---XYZ __IN__ my $expected = <<'__EXP__'; Column1 Column2 Column3 Column4 A B C PQR A B C XYZ A D LMN A XYZ __EXP__ my (@path,@table); my $max=0; for (split /\n/, $input) { #print ; /^ (.*?) (---(\w\w\w))? $ /x; my $leave = $3; my @track = split / |--/, $1; die "parsing error $_" unless @track; $path[$#track] = $1 if ($track[-1] =~ /(\w)/ ); #pp [@track], [@path], $leave; if ($leave) { push @table, [ @path[0..$#track], $leave ]; $max = @track if @track >$max; } } pp \@table; my $out=""; $out .= "Column$_\t" for 1..$max+1; $out .= "\n"; for (@table){ my $width = @$_-1; $out .= join "\t", @$_[0..$width-1]; $out .= "\t"x($max-$width+1); $out .= @$_[$width]; $out .= "\n"; } print $out; is($out,$expected);

1..1 [ ["A", "B", "C", "PQR"], ["A", "B", "C", "XYZ"], ["A", "D", "LMN"], ["A", "XYZ"], ] Column1 Column2 Column3 Column4 A B C PQR A B C XYZ A D LMN A XYZ

Cheers Rolf

( addicted to the Perl Programming Language)


In reply to Re: Parsing a Tree to a Table. by LanX
in thread Parsing a Tree to a Table. by choroba

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
  • Outside of code tags, you may need to use entities for some characters:
            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 perusing the Monastery: (16)
    As of 2014-10-21 14:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (105 votes), past polls