0

I need to get a specific field from a CSV file and put it in an array. I am not sure how to do this. This is what I have tried so far.

#!/usr/bin/perl

use strict;
use warnings;

my @array  = <>;
my @fields = split ",", @array;

print @fields[2];

This is an example of the CSV file

9988,Kathleen,Brown,[email protected],OH,Female,Italian
9989,Antonio,Ford,[email protected],IL,Male,
9990,Diana,Banks,[email protected],MA,Female,English
1
  • So you want a list of all the surnames?
    – Borodin
    Mar 12, 2015 at 23:21

2 Answers 2

3

If there is any chance that your CSV file contains quoted fields (so that each field may itself contain a comma) then you should use Text::CSV to handle the data properly. However, for simple data like that in your question, it is fine to use just split.

Your code would look something like this. Note that it is usually unnecessary to read an entire file into memory, and line-by-line processing is more memory-efficient. It also tends to focus the programmer's attention on a single line and hence improve the resulting design.

use strict;
use warnings;

my @names;

while ( <> ) {
  chomp;
  my @fields = split /,/;
  push @names, $fields[2];
}

print "$_\n" for @names;

output

Brown
Ford
Banks

Update

If you are comfortable with map then you may prefer this. It is much more concise, but suffers from the same inefficiency as your own code in that it reads the whole file into memory at once (although it discards it again immediately). Unless the file is enormous that shouldn't be a problem.

use strict;
use warnings;

my @names = map { chomp; ( split /,/ )[2]; } <>;

print "$_\n" for @names;
-1

There is a perl module that handles many file formats including csv. You can install the module by running:

$ sudo cpan install Text::CSV;

Now you'll be able to easily have the needed parsing of your comma delimiter (which is the default) or specify any other character.

After installing the perl module, this is a quick script to achieve your task. I created a text file with your data called test.csv.

#!/usr/bin/perl

use strict;
use warnings;
require Text::CSV;

my $csv = Text::CSV->new;

open (DATA, "<test.csv") or die "Can't open file...";
while (<DATA>) {
    $csv->parse($_);
    my@fields = $csv->fields(); 
    print $fields[2];
}
close DATA;

You can see other features of the Text::CSV module reviewing the documentation by running:

$ perldoc Text::CSV
3
  • Don't mess with your system's perl like that. If you have sudo, and your distribution has a package for the module, use that. Otherwise, just use one of the many alternatives that keep's your system's perl out of harm's way. Mar 13, 2015 at 0:52
  • I spent time testing my recommendation when answering the question. It appeared Borodin had a similar hint of this valuable perl module in his answer, which I noticed after I posted my answer. It happened that while testing the recommendation, the module wasn't installed on the machine I was using for testing. I would imagine a user with sudo access might be installing programs (such as perl) for system wide use. In my case I use my perl scripts to run many system tasks. My programs/tools would be broken if perl wasn't upgraded system wide. Mar 13, 2015 at 4:49
  • Some people are rude when it comes to down vote. I spent a lot of time testing an easy workable solution. It's not off topic. It'll work very well. It also includes a line to show the user how to use the very extensive CSV features. If I had spent less time formatting to make my answer clear and easy to follow, it would have been the first posted answer and most likely have received the original check mark. I commend the guru who typed faster. His answer suggested merit of the module. My answer show exactly how to use it, of which I'm sure many people will benefit. Mar 13, 2015 at 22:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.