Suggestions for perl resources? Dealing with csv data.

Reply

Join Date: May 2006
Posts: 11
Reputation: heatherc3 is an unknown quantity at this point 
Solved Threads: 0
heatherc3 heatherc3 is offline Offline
Newbie Poster

Suggestions for perl resources? Dealing with csv data.

 
0
  #1
May 18th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 11
Reputation: heatherc3 is an unknown quantity at this point 
Solved Threads: 0
heatherc3 heatherc3 is offline Offline
Newbie Poster

Re: Suggestions for perl resources? Dealing with csv data.

 
0
  #2
May 18th, 2006
And I'll be using array variables, right?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,327
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 248
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Suggestions for perl resources? Dealing with csv data.

 
0
  #3
May 19th, 2006
Well to split one line into an array it would go like this
  1. my @line = split(/\s*,\s*/, $inputLine);

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

  1. open(FH, ......);
  2. while(<FH>) {
  3. my @line = split(/\s*,\s*/, $_);
  4. ..... work on this line ....
  5. }
  6. 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Suggestions for perl resources? Dealing with csv data.

 
0
  #4
May 19th, 2006
There is a perl module for handling csv files:

Text::CSV

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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 11
Reputation: heatherc3 is an unknown quantity at this point 
Solved Threads: 0
heatherc3 heatherc3 is offline Offline
Newbie Poster

Re: Suggestions for perl resources? Dealing with csv data.

 
0
  #5
May 19th, 2006
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.'
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Suggestions for perl resources? Dealing with csv data.

 
0
  #6
May 19th, 2006
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().
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Suggestions for perl resources? Dealing with csv data.

 
0
  #7
May 19th, 2006
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:
  1. $firstpart = $line[0];
  2. $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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Suggestions for perl resources? Dealing with csv data.

 
0
  #8
May 19th, 2006
That's correct, but you can also assign a list of scalars to the split:

  1. 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:

  1. @list = split(/,/,$line);
  2. if ($list[0] eq 'foo') {
  3. ....
  4. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Suggestions for perl resources? Dealing with csv data.

 
0
  #9
May 19th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC