#!/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);