This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Convert A Matrix Into A Hash Of Arrays

Im trying to convert a big matrix (a tab separated file with different number of elements in each column) into a hash of arrays. In the first step, I succesful load the file and using Test::CSV convert the columns of the data into lists, but in doing so, I noted that the lengh of each list is the number of elements correspont to the larger column, i.e., for those columns with less elements, a blank space is present. So far, this is my current code:

#!/usr/bin/perl
use warnings;
use strict;
use Text::CSV;

my $csv = Text::CSV->new({
  sep_char => "\t",
  });

open( LIST, "<", "testfile" ) or die "No esta el archivo\n";


while (<LIST>) {
        if ($csv->parse($_)) {
    my   @columns = $csv->fields();
          print "$columns[0]\t$columns[1]\t$columns[2]","\n";
          } else {
        my $err = $csv->error_input;
    }
}
close(LIST);

The input matrix have 20 columns and between 4500-8500 rows plus header row (which ideally I would like to use as keys in the hash). For simplicity I build a "testfile" with three columns, no header and different number of elements (in the same format as the original input file). Here is the content of testfile:

1 1 2
2 2
  3 4
5 6
6 7
7 8 8
8 9

This is the output. I presume that the "Use of uninitializated value..." is related to the blank spaces.

1    1    2
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 3.
2    2    
    3    4
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 5.
5    6    
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 6.
6    7    
7    8    8
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 8.
8    9    
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 9.
Use of uninitialized value in concatenation (.) or string at blast.cruce.especies.pl line 16, <LIST> line 9.
Any help are welcome.

Christian.

matrix

What does this have to do with bioinformatics? You might try stackoverflow.

Not a bioinformatics question. Closing.

my mistake, sorry. thanks for closing it.

0 answers

No answers yet.

Log in to answer this question.