Transpose in perl

前端 未结 9 1668
一向
一向 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:53

    how about Array-Transpose-0.06 ?

    http://metacpan.org/pod/Array::Transpose

    0 讨论(0)
  • 2020-12-09 18:55

    Here's an outline of one way to transpose data. Working through this example will be instructive because you will need to use CPAN, you will learn about the useful List::Util and List::MoreUtils modules, you will learn the basics of complex data structures (see perlreftut, perldsc, and perllol), and you will get to use an iterator in Perl.

    use strict;
    use warnings;
    use List::MoreUtils qw(each_arrayref);
    
    my @raw_data = (
        '0 1 2 3 4 5 6 7 8 9 10',
        '6 7 3 6 9 3 1 5 2 4 6',
    );
    
    my @rows = ... ; # Look up map() and split() to fill in the rest.
                     # You want an array of arrays.
    
    my @transposed;  # You will build this in the loop below.
    
    my $iter = each_arrayref(@rows);  # See List::MoreUtils documentation.
    
    while ( my @tuple = $iter->() ){
        # Do stuff here to build up @transposed, which
        # will also be an array of arrays.
    }
    
    0 讨论(0)
  • 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}'
    
    0 讨论(0)
  • 2020-12-09 19:00
    use strict;
    my ($i, $rows, $cols) = (0, 10, 100);
    # initiate array 10x100
    my $array = [map {[map {$i++} (1..$cols)]} (1..$rows)];
    # transpose array into 100x10 array
    my $transpose = [map {[map {shift @$_} @$array]} @{$array->[0]}];
    

    array must be a matrix, i.e columns must be equal for each row, original array will be destroyed

    this code will not use extra memory for transpose, x2 for other libraries, for large array for example 100x1M, it matters

    0 讨论(0)
  • 2020-12-09 19:06
    use List::UtilsBy qw/zip_by/;
    
    my @transposition = zip_by { [ @_ ] } @matrix;
    

    https://metacpan.org/pod/List::UtilsBy#zip_by

    0 讨论(0)
  • 2020-12-09 19:08
    use strict;
    # read the first line
    my @labels = split ' ', <>;
    # read and ignore the empty second line
    <>;
    # read the third line
    my @values = split ' ', <>;
    # transpose (I suppose you'll do more with the table than just printing it)
    my %table = map { $labels[$_] => $values[$_] } 0..$#labels;
    # print
    foreach (@labels) {
        print "$_ $table{$_}\n";
    }
    
    0 讨论(0)
提交回复
热议问题