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 7584875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:01:54+00:00 2026-05-30T19:01:54+00:00

I would like to transform the following lines of data into a structured table

  • 0

I would like to transform the following lines of data into a structured table with perl:

3=884|4=884|5=MBREFUSDCMR|25=113500|824=20120229|387=4.15|248=TLD|280=5
3=884|4=884|5=MBREFUSDCMR|24=1|12=0|10=0|25=113500|824=20120229|280=5
3=884|4=884|5=MBREFUSDCMR|24=1|270=252304|280=5

Each value before the equal sign represents the identifier of the
corresponding value. Now I like to produce a table for further
analysis. The first row should include the identifiers and below their values. There is an unequal amount of identifiers in the
three rows which have to be kept in mind. Missing values should be
filled with the string “NA”. So it should look like this

3|4|5|24|12|10|25|824|387|248|270|280
884|884|MBREFUSDCMR|NA|NA|NA|113500|20120229|4.15|TLD|NA|5
884|884|MBREFUSDCMR|1|0|0|113500|20120229|NA|NA|NA|5
884|884|MBREFUSDCMR|1|NA|NA|NA|NA|NA|NA|252304|5

@DVK

My approach was to first get all the variables. That would be the header/first row for the table. E.g.

my @data_dirty = <STDIN>;
chomp(@data_dirty);

## get the columns names
my ( @tmp, @var );
foreach my $j (@data_dirty) {
    foreach my $i (split /\|/, $j) {
    $i =~ s/\[.*\]//g;
    $i =~ s/\s+//g;
    $i =~ s/(.*)=.*/$1/g;
    push(@tmp, $i);
    }
}
@var = uniq @tmp;

Afterwards I might check each row if the variable in @var exists, if yes write the corresponding value otherwise write an “NA”. However, I had some troubles with the checks and the correct storing of the data so that afterwards the output looks as desired.

  • 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-05-30T19:01:56+00:00Added an answer on May 30, 2026 at 7:01 pm

    The key to addressing such needs is to divide and conquer. A careful choice of subroutines is all you need.

    Here, data needs to be loaded and all identifiers need to be known a priori before any table is printed.

    The example below doesn’t order the columns by their sequence of appearance (left as an exercise for the reader):

    use strict;
    use warnings;
    
    my ( $data, $headers ) = load_data( 'tabular_data.txt' );
    
    print_tabular( $data, $headers );
    
    sub load_data {
    
        my ( $file ) = @_;
    
        open my $fh, '<', $file or die $!;
    
        my ( @records, %fields );
        while ( my $line = <$fh> ) {
    
            chomp $line;
    
            my @columns = split /\|/, $line;               # Get columns
            my %entries = map { split /=/, $_ } @columns;  # Populate record,
                                                           # keys = fields
            push @records, \%entries;                      # Add to data collection
    
            $fields{$_}++ for keys %entries;               # Detect new headers
        }
    
        close $fh;
    
        return ( \@records, [ keys %fields ] );
    }
    
    sub print_tabular {
    
        my ( $data, $headers ) = @_;
    
        pretty_print( @$headers );
    
        for my $record ( @$data ) {
            my @values = map { exists $record->{$_}  # exists check needed...
                                 ? $record->{$_}     # ... otherwise header '0'
                                 : 'NA'              # ... would always print 'NA'
                             } @$headers;
            pretty_print( @values );
        }
    }
    
    sub pretty_print { print join( '|', @_ ), "\n" }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use xslt to transform an xml file into an almost
I would like to transform the following nested for loop first <- c(1, 2,
I have the following code that uses a for loop and I would like
I am using xsl to transform xml to kml format. I would like to
I want to translate an XML file with data like the following: <FlatData> <Details1_Collection>
I am using Ruby on Rails v3.0.9 and I would like to transform an
Suppose I would like to match for the following pipe-delimited strings: std::string re(a|*|c); std::string
Im writing a compiler for university project, and I would like to transform my
I would like to know, wether Server.Transfer will work in a Web Farm. Thanking
In my Java application I would like to download a JPEG, transfer it to

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.