Perl code for replacing columns with different texts

Source: Internet
Author: User

Sometimes it is troublesome to encounter different lines or columns with different lengths. Although you can fill the file in R with the longest rows (columns), it is inconvenient, so I think of using perl to implement one.
A text file of an indefinite length separated by commas (,) must be transposed by columns.

Copy codeThe Code is as follows: 1, 2, 3, 4, 5, 6,
7, 8, 9,
10, 11, 12, 13,
Convert:
1, 7, 10,
2, 8, 11,
3, 9, 12,
4, 13,
5 ,,,
6 ,,,

The following is the complete code for your reference.

Copy codeThe Code is 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,

After preparation and learning, implement it by yourself. It is best to make a template and use it as needed.

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.