Perl implementation code for permutation of rows of different lengths of text _perl

Source: Internet
Author: User

Sometimes it is troublesome to encounter rows or columns that are inconsistent in length, although you can fill the file with the longest row (column) in R, but it is inconvenient, so remember to use Perl to achieve one.
The indefinite length text file separated by commas is now implemented as a row-and-column transpose, which is

Copy Code code as follows:

1,2,3,4,5,6,
7,8,9,
10,11,12,13,
Convert to:
1,7,10,
2,8,11,
3,9,12,
4,, 13,
5,,,
6,,,

The following is the complete code for your reference.

Copy Code code as follows:

#!/usr/bin/perl-w
My @matrix;
My $max _len = 0;
while (<DATA>) {
Chomp
s/,$//g;
My @fields = Split/,/, $_;
my $len = @fields;
$max _len = $max _len > $len? $max _len: $len;
Push @matrix, [@fields];
}
For my $col (0. $max _len-1) {
For my $line (@matrix) {
Print $line->[$col] | | ' ', ',';
}
print "\ n";
}
__data__
1,2,3,4,5,6,
7,8,9,
10,11,12,13,

Ready to learn, do it yourself, the best is to make a template, Tiaogan with.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.