I have the input format as below, I want to create a tabular format for these data.
CELL = "abc"
"model" "abc"
"description" "qwerty+keypad with slide"
**tech**
size (big \$l \$w m)
termOrder (x y z)
namePrefix "S"
prop (nil \$l l \$w w)
**spec**
term (nil C \:1 B \:2 E \:3)
termOrder (x y z)
***********************************************************
CELL = "efg"
"model" "efg"
"description" "touchscreen+qwerty no slide"
**tech**
size (small \$l \$w m)
termOrder (x y z)
namePrefix "S"
prop (nil \$l l \$w w)
**spec**
term (nil x \:1 y \:2 z \:3)
termOrder (x y z)
I want a table with names on left as headers and the data on the right to be its values.
. tech spec
CELL model description size termOrder namePrefix prop termOrder Term
These are the headers and I want the corresponding values below these headers.
I tried using this code which I had used for another kind of tabulation:
my $pr = "%-12s";
my @headers = qw/............../;
my %names;
while (<DATA>) {
chomp;
my $line = <DATA>;
%{$names{$_}} = split /=|\s+/, $line;
}
printf $pr x @headers . "\n", @headers;
for (keys %names) {
my @ds = ($_);
for my $k (@headers[1 .. $#headers]) {
my $v = $names{$_}->{$k};
push @ds, $v ? $v : '-';
}
printf $pr x @ds . "\n", @ds;
}
This doesn’t yield a required result, so kindly help me out with this.
This is really not the easiest task and like always, there’s more than one way to do it. Here’s one. If there are any questions, feel free to ask because it’s really too much code to explain everything.
However, if it was my task, I would have chosen HTML as the output format to get rid of all these width calculations – also there are comfortable JS tools to sort those tables. If you really want to do things like this with text only, maybe “good old formats” are for you. 😉
Code
Output