I'm a n00bly n00b when it comes to Perl.

I have data that will be set out in comma separated values that needs to have logic applied to it and then replace the value with text. This is to prep the data for a mail merge.

We're using this to generate letter for students - telling them that they're admitted to a program, for example. The perl needs to look at one or two (or three or four) pieces of data in the csv and make a decision...so I imagine I'll be using some if and elseif statements to apply the logic.

I was wondering if there were suggestions on good resources? How do I specify positions in the csv in my perl code?

Recommended Answers

All 8 Replies

And I'll be using array variables, right?

Well to split one line into an array it would go like this

my @line = split(/\s*,\s*/, $inputLine);

to read an entire file this way it would be as so:

open(FH, ......);
while(<FH>) {
  my @line = split(/\s*,\s*/, $_);
  ..... work on this line ....

this is of course not doing any kind of error handling whatsoever, but
this should get you started.

There is a perl module for handling csv files:


but it's not a core module so would maybe would need to be installed. It's pretty common to have it installed since dealing with csv file is pretty common. Ask tech support if you are unsure.

Thanks for the advice. I appreciate it!

Can I work with the values in the csv file line by line without splitting it? I want it to look at the data in one comma-separated value and compare it to another one in the same row, and then replace text in the first if it meets a condition.

Each line represents data for an individual student. If position 1 in the line says Y and position two in the line says 85, I want to replace the text in position one with the statement 'you've been admitted and you've received a scholarship.' If position 1 says Y and position 2 says 75, I want the text in position 1 to be replaced with 'you've been admitted.'

you can probably do what you want without splitting the data into fields but whats the problem with splitting it? Thats the whole idea behind having delimited data in the first place, so you can work with the specific fields of data. Your altrernative is probably to use regexps or maybe substr().

You want to split the data.... I'm guessing you are unsure how to extract the values from the split data... so add this to the above:

$firstpart = $line[0];
$secondpart = $line[1];

Someone correct me if I'm wrong, but that will assign the first and second field to variables. Now you can test if ($firstpart eq "Y") { and then do your replace on the file......right?

That's correct, but you can also assign a list of scalars to the split:

my ($name,$address,$phone,$zip,$other) = split(/,/,$line);

then just use the appropriate scalar, or just use the array index, there really is no need to assign the array index to another scalar unless you really wanted to:

@list = split(/,/,$line);
if ($list[0] eq 'foo') {

Right, I just did that for the readablity issue. I probably would have done the first suggestion, and just assigned scalars to the initial split.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.