tags:

views:

125

answers:

3

I am creating a script to read values from csv files and use the values for other taskes. I have written the below code to read values.

sample file:

site,type,2009-01-01,2009-01-02,....
X,A,12,10,...
X,B,10,23,...
Y,A,20,33,...
Y,B,3,12,...

and so on....

Code:

my @value;
while (<INFILE>) {
    next if $_ !~ /B/;
        my ($v1, $v2, @v3) = split /[,]/, $_;
        push(@value, @v3);
}

it gives me all the values of type B. I need help to create different arrays for each type B values.

+2  A: 

From what I comprehend, you want to use a list of lists:

my @value;
while (<INFILE>) {
    next if $_ !~ /B/;
    chomp;
    my ( $v1, $v2, @v3 ) = split /[,]/, $_;
    push @value, [@v3];  # This creates a list of lists
}

use Data::Dumper::Simple;

print Dumper @value;
Alan Haggai Alavi
+1  A: 

Please have a look at this link. Hope this helps you.

Space
`perldoc perllol` is good, though I find `perldoc perldsc` even more helpful (though more complex. Note that you can get the latest version of all of Perl's documentation online here http://perldoc.perl.org/. You should also be able to get the documentation in a terminal at any time (assuming a reasonably standard Perl installation - though some operating systems remove the docs).
Telemachus
+3  A: 

Reading CSV files is harder than most of us thought at first. It even turns out that reading CSV files is frustratingly hard. Thus my recommendation is to not do this yourself, but to use Text::CSV_XS instead.

innaM
It looks so simple at first, but then it's not.
Telemachus