Transpose in perl

前端 未结 9 1709
一向
一向 2020-12-09 18:41

I have started learning perl and like to try out new things.

I have some problem in text processing. I have some text of the form,

0 1 2 3 4 5 6 7 8          


        
9条回答
  •  攒了一身酷
    2020-12-09 18:57

    Here's my new script to transpose a tab-delimited file. Change \t to your delimiter if you like.

    #!/usr/bin/perl -anF/\t|\n/
    $n = @F - 1 if !$n;
    for $i (0..$n) {
        push @{ $m->[$i] }, $F[$i];
    }
    END {
        for $r (@$m) {
            print join("\t", @$r), "\n";
        }
    }
    

    or as a 104 character "one liner" (with apostrophe-backslash-newline-apostrophe added to avoid horizontal scrolling):

    perl -anF'\t|\n' -e'$n=@F-1if!$n;for(0..$n){push@{$$m[$_]},$F[$_]}'\
    'END{print map{join"\t",@$_,"\n"}@$m}'
    

提交回复
热议问题