Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8578115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:18:48+00:00 2026-06-11T20:18:48+00:00

I have the input format as below, I want to create a tabular format

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:18:49+00:00Added an answer on June 11, 2026 at 8:18 pm

    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

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    use feature 'switch';
    use List::Util 'sum';
    
    # preparations
    my @blocks; # array for all data block
    my $block;  # the data block we're working with
    my $part;   # the data block part we're working with
    
    # read things and decide what to do
    for (<DATA>) {
        chomp;
    
        # start of a new data block, first part: main
        when (/CELL\s*=\s*"?([^"]+)"?/) {
            $part   = 'main';
            $block  = {
                $part   => {CELL => $1},
                tech    => {},
                spec    => {},
            };
            push @blocks, $block;
            next;
        }
    
        # start a new part
        when (/\*\*(tech|spec)\*\*/) {
            $part = $1;
            next;
        }
    
        # fill parts
        when (/"?(\w+)"?\s+"?([^"]+)"?/) {
            $block->{$part}{$1} = $2;
            next;
        }
    }
    
    # prepare output
    my %columns = (
        main => [
            {name => 'CELL',        length =>  5},
            {name => 'model',       length =>  5},
            {name => 'description', length => 30},
        ],
        tech => [
            {name => 'size',        length => 20},
            {name => 'termOrder',   length => 10},
            {name => 'namePrefix',  length => 10},
            {name => 'prop',        length => 20},
        ],
        spec => [
            {name => 'term',        length => 30},
            {name => 'termOrder',   length => 10},
        ],
    );
    
    # part legend
    foreach my $part (qw(main tech spec)) {
        my $width = sum map {$_->{length} + 2} @{$columns{$part}};
        print $part . ' ' x ($width - length $part);
    }
    print "\n";
    
    # column legend
    foreach my $part (qw(main tech spec)) {
        foreach my $column (@{$columns{$part}}) {
            my ($name, $length) = @{$column}{qw(name length)};
            print $name . ' ' x ($length - length($name) + 2);
        }
    }
    print "\n";
    
    # print each block in columns
    foreach my $block (@blocks) {
        foreach my $part (qw(main tech spec)) {
            foreach my $column (@{$columns{$part}}) {
                my $value = $block->{$part}{$column->{name}};
                print $value . ' ' x ($column->{length} - length($value) + 2);
            }
        }
        print "\n";
    }
    
    __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)
    

    Output

    main                                          tech                                                                spec                                        
    CELL   model  description                     size                  termOrder   namePrefix  prop                  term                            termOrder   
    abc    abc    qwerty+keypad with slide        (big \$l \$w m)       (x y z)     S           (nil \$l l \$w w)     (nil C \:1 B \:2 E \:3)         (x y z)     
    efg    efg    touchscreen+qwerty no slide     (small \$l \$w m)     (x y z)     S           (nil \$l l \$w w)     (nil x \:1 y \:2 z \:3)         (x y z)     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data in a mysql table in long / tall format (described below)
I have a text file input which contains data as below. How can I
I have a form input in my application that accepts dates in the format
I have a asp.net page with Date input. It accepts date in the format
Hi I have the following date as String format. Input 2010-04-20 05:34:58.0 Output I
I have input.txt and I want to search for a string and extract a
i have input xml as <content> <date> <day>14</day> <month>06</month> <year>2012</year> </date> </content> want it
I have an input string in the following format String input = 00IG356001110002005064007000000; Characters
I have some input fields like below: <input type=text name=date_1 class=dataList value=2009-12-30 /> <input
Drupal version 6.12 I have a page whose input format is PHP. I simply

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.