Question: (Closed) Convert A Matrix Into A Hash Of Arrays
0
gravatar for chodar
4.5 years ago by
chodar10
Chile
chodar10 wrote:

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 • 1.6k views
ADD COMMENTlink written 4.5 years ago by chodar10

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

ADD REPLYlink written 4.5 years ago by Devon Ryan84k

Not a bioinformatics question. Closing.

ADD REPLYlink written 4.5 years ago by Chris Miller20k

my mistake, sorry. thanks for closing it.

ADD REPLYlink written 4.5 years ago by chodar10
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1592 users visited in the last hour