LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2008, 01:46 PM   #1
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Rep: Reputation: 15
Perl programming assistance required


I have a small program that reads in a csv file, truncates a line or 2 using split. Makes a calculation, and rearrange the output and place it into another csv file.

The problem lies in where the "" end up. Apparently the truncation screws up the placement of quotation Marks. How do I strip the "" from the file as i read it in? This will solve the issue.
 
Old 03-05-2008, 02:40 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Do you mean something like this (obviously, $value would be read in from a file):

Code:
#!/usr/bin/perl

$value = '"addf,as,df,123"';
$value =~ s/"//g;
($dummy1, $dummy2, $dummy3) = split(",",$value);
print "$dummy3,$dummy2,$dummy1\n";
HTH

Forrest
 
Old 03-05-2008, 02:46 PM   #3
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
ty ty ty so very much
 
Old 03-05-2008, 03:04 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by forrestt View Post
Do you mean something like this (obviously, $value would be read in from a file):

Code:
#!/usr/bin/perl

$value = '"addf,as,df,123"';
$value =~ s/"//g;
($dummy1, $dummy2, $dummy3) = split(",",$value);
print "$dummy3,$dummy2,$dummy1\n";
HTH

Forrest
Doesn't this blow away the reason for the quotes? What happens if the d-quotes are enclosing one or more commas?
The example given is not typical CSV format. Normally, there are quotes enclosing individual text fields so that commas and whitespace can be preserved within. It seems to me you would be better served with one of perl's fancy extended regex's which can match commas that are immediately enclosed by quotes.
It will probably require a combination of splits, depending on which fields are quoted.
Maybe this complexity is why there is the perl module Text::CSV.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script assistance required.. TheEngineer Linux - Newbie 4 10-26-2006 06:42 AM
Required Assistance in Mandriva tuannie Linux - Software 6 07-06-2006 11:16 AM
assistance required v.s.sankar Linux - Certification 4 05-02-2006 08:01 AM
NVidia assistance required Hubmasterflex Linux - Hardware 9 01-27-2006 10:36 PM
Gotomail Configuration assistance required jefx Linux - Software 4 05-04-2003 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration