| | |
Suggestions for perl resources? Dealing with csv data.
![]() |
•
•
Join Date: May 2006
Posts: 11
Reputation:
Solved Threads: 0
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?
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?
Well to split one line into an array it would go like this
to read an entire file this way it would be as so:
this is of course not doing any kind of error handling whatsoever, but
this should get you started.
Perl Syntax (Toggle Plain Text)
my @line = split(/\s*,\s*/, $inputLine);
to read an entire file this way it would be as so:
Perl Syntax (Toggle Plain Text)
open(FH, ......); while(<FH>) { my @line = split(/\s*,\s*/, $_); ..... work on this line .... } close(FH);
this is of course not doing any kind of error handling whatsoever, but
this should get you started.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: May 2006
Posts: 11
Reputation:
Solved Threads: 0
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.'
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 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:
Someone correct me if I'm wrong, but that will assign the first and second field to variables. Now you can test
Perl Syntax (Toggle Plain Text)
$firstpart = $line[0]; $secondpart = $line[1];
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:
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:
Perl Syntax (Toggle Plain Text)
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:
Perl Syntax (Toggle Plain Text)
@list = split(/,/,$line); if ($list[0] eq 'foo') { .... }
![]() |
Similar Threads
- Perl CSV question (Perl)
Other Threads in the Perl Forum
- Previous Thread: Help with Perl script to verify IP addresses
- Next Thread: perl in bioinformatics
| Thread Tools | Search this Thread |






