Transpose in perl

前端 未结 9 1669
一向
一向 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 19:08

    I ran across this question while searching for a way to transpose an AoA I built by reading a file, but of course, this solution will still work for any AoA, and it doesn't require the array references have equal numbers of elements. I know this is an old question, but as I'm still learning, it would be awesome to have some critique/examples of how to improve my solution.

    my $max_rows = 0;
    foreach my $line (@copy) {
        my $ref = ();
        push @$ref, split(/\s+/, $line);
        my $rows = scalar(@$ref);
        $aoa[$i] = \@$ref;
        $i++;
        if ($rows > $max_rows) {
            $max_rows = $rows;
        }
    }
    
    for (my $i = 0; $i <= $max_rows; $i++) {
        my @aoa_t = ();
        for (my $k = 0; $k <= $#aoa; $k++) {
            if ($aoa[$k]->[$i]) {
                push @aoa_t, $aoa[$k]->[$i];
            } else {
                push @aoa_t, " ";
            }
            print " @aoa_t\n";
        }
        if ($aoa_t[0] eq " ") {
            last;
        } else {
            $t_aoa[$i] = \@aoa_t;
        }
    }
    

    It worked very well for a file with 200+ lines of data in space delimited format. Any feedback is appreciated, if that is appropriate in this forum. Regards.

    0 讨论(0)
  • 2020-12-09 19:12

    There certainly is, and Mike has pointed out the simplest way. If you are learning, you probably want to write your own function?
    First, you want to split each line on spaces to get an array of values (or push the list of words into the array, as in Dalton's answer; in Perl, there's always more than one way to do anything)
    Then, for each element in the array, you want to print it and its counterpart in the second array onto the same line. (What will you do if one array runs out before the other?)

    Of course, if you want to learn Perl, you will also definitely want to learn to use CPAN as well, so it's still worthwhile to try using Data::Pivot.

    0 讨论(0)
  • 2020-12-09 19:17

    So this solution uses an array-of-arrays, each nested array is a row of data. Very simply you loop over the columns in each row and push them onto another array-of-arrays using the column index as the index which to push the value onto. This has the effect of pivoting the data as you requested.

    #!/usr/bin/env perl
    
    my @rows = ();
    my @transposed = ();
    
    # This is each row in your table
    push(@rows, [qw(0 1 2 3 4 5 6 7 8 9 10)]);
    push(@rows, [qw(6 7 3 6 9 3 1 5 2 4 6)]);
    
    for my $row (@rows) {
      for my $column (0 .. $#{$row}) {
        push(@{$transposed[$column]}, $row->[$column]);
      }
    }
    
    for my $new_row (@transposed) {
      for my $new_col (@{$new_row}) {
          print $new_col, " ";
      }
      print "\n";
    }
    

    This results in:

    0 6 
    1 7 
    2 3 
    3 6 
    4 9 
    5 3 
    6 1 
    7 5 
    8 2 
    9 4 
    10 6
    
    0 讨论(0)
提交回复
热议问题