0

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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

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;

Comments

1

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

1 Comment

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 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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.