Bsquared169 has asked for the wisdom of the Perl Monks concerning the following question:

I am a but a fledgling perl monk, as its my first day. I have a couple questions.

How do I read a csv file?

How do I extract one column of strings in a csv file into an array?

After I can do the above I will use a for loop inside a for loop to compare element 1 of array 1 with all elements of array 2. If any strings are equal I will take the string from array 3 and copy it into the first element of array 4.

Then, How do I print array 4 back into the blank column of my csv file.

Replies are listed 'Best First'.
Re: Parse CSV file
by Tux (Canon) on Aug 04, 2015 at 21:52 UTC

    Use the module Text::CSV_XS.

    use Text::CSV_XS "csv"; # read only column 3 my $aoa = csv (in => "file.csv", fragment => "col=3"); # convert to array of strings my @list = map { $_->[0] } @$aoa;

    update: in perl6 that would be:

    use Text::CSV; my @list = csv(in => "file.csv", fragment => "col=3").map(*[0]);

    Or, better readable (IMHO):

    use Slang::Tuxic; use Text::CSV; my @list = csv (in => "file.csv", fragment => "col=3").map (*[0]);

    Enjoy, Have FUN! H.Merijn
Re: Parse CSV file
by Laurent_R (Canon) on Aug 05, 2015 at 08:55 UTC
    I will use a for loop inside a for loop to compare element 1 of array 1 with all elements of array 2
    Nested for loops over two arrays might not be the best option. Consider loading the data of array 2 into a hash and using a look-up into the hash. This should be much faster and also easier to code.

    You don't give enough details for me to be able to be more specific, but "array 3" might also be better designed as a hash.

Re: Parse CSV file
by Anonymous Monk on Aug 05, 2015 at 02:07 UTC
    Beyond that ... koff koff ... it sounds like homework.
Re: Parse CSV file
by taiko (Sexton) on Aug 06, 2015 at 08:35 UTC