Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear All, I just begin to learn perl to solve some biology problem. I have a matrix(rows400*columns4000), I only want to print out a new matrix which have all rows but part of the columns like: 1,2,3,5,9,10,38,..126,....,(around 1660 columns) those columns distribute no rules at all. How can I get into it? Please help and Thanks advance!

Replies are listed 'Best First'.
Re: How can I print out part of the matrix
by mfriedman (Monk) on Jun 02, 2002 at 20:44 UTC
    How are you building your matrix? If you're using an array of arrays, simply loop through the outer array and print the data in the inner array:

    #!/usr/bin/perl -w use strict; my @aoa = get_data; foreach my $row(@aoa) { print "$row->[$_] " for (1, 2, 3, 5, 9, 10, 38, 126); print "\n"; }

    If you don't know what columns you need to print ahead of time, build an array of the indices you need and use

    print "$row->[$_] " for @indices;

    -Mike

      But that means you're making many calls to print. Why not just:
      print "@{$_}[1, 2, 3, 5, 9, 10, 38, 126]\n" for @aoa;

      Abigail

Re: How can I print out part of the matrix
by clintp (Curate) on Jun 02, 2002 at 20:46 UTC
    The question is, do you know how to print the matrix at all? I'm going to assume you can, and if so instead of:
    # pseudocode for row (0..rows) { for col (0..cols) { print array[row][col] } }
    You substitute a list for the colums and rows you want to iterate over:
    # pseudocode for row (@goodrows) { for col (@goodcols) { print array[row][col] } }


    As an aside, this is interesting...
    @gm=map { [ @$_[@goodcols] ] } @matrix[@goodrows];
    Giving a matrix with only the good columns and rows in it. Hmm. Although I'd have preferred some kind of @matrix[@goodrows]->[@goodcols] syntax, but I'll have to wait for Perl 6 for that.