Off topic:Convert A Matrix Into A Hash Of Arrays
0
0
Entering edit mode
10.1 years ago
chodar ▴ 10

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 • 2.8k views
ADD COMMENT
This thread is not open. No new answers may be added
Traffic: 1913 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6