Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split but ignore between quotes

Status
Not open for further replies.

jaytco

Vendor
Dec 13, 2001
88
US
I have a text flat file that is comma seperated so I was using @lineArray = split(/,/,$line); , but most of the fields are double quoted, and there are comma's inside the double quote that I would like split to ignore. The red comma I would like to ignore.

Thanks

"C&E MAINT","3650TH MAINT CO","113-02","2320-00-050-8984","TRK, TRAC M818
 
If you split on "," this will only split up the commas that are immediately separated by double quotes.

You will have an extra quote at the beginning and end of the array but that can easily be replaced.

HTH

----------------------------
SnaveBelac - Adventurer
 
In order to handle the problem in a comprehensive manner, do what you should nearly always do, use a module. Text::CSV

Here is some code that would bust out the fields into an array called @columns. The line to parse would be in $line in this example.
Code:
use Text::CSV;

 $version = Text::CSV->version();      # get the module version

 $csv = Text::CSV->new();              # create a new object

 $status = $csv->parse($line);         # parse a CSV string into fields
 @columns = $csv->fields();            # get the parsed fields
--jim
 
Thanks for the idea on the Text::CSV module. Never used it, but will give it a shot.

HTH, as far as your example, I thought about that, but I do have some fields that do not have quotes (numerical values).

I also have some fields that have no string value ,"", or may have no numerical data ,,,.

"C&E MAINT","3650TH MAINT CO","113-02","2320-00-050-8984","TRK, TRAC M818","","",,,1.0000,1.0000
 
HTH --> Hope that helps (SB)
Benefactoring Contributions-->SnaveBelac & Coderifous
Not getting it--> Y O U

--Paul
PS The daemons wake . . .
 
Code:
#!/usr/bin/perl

$string = '"C&E MAINT","3650TH MAINT CO","113-02","2320-00-050-8984","TRK, TRAC M818","","",,,1.0000,1.0000"';

$string =~ s/([A-Z ]),([A-Z ])/$1•$2/g;

$string =~ s/"//g;

@array = split (",", $string);

foreach (@array) {
  chomp;
  s/•/,/g;
  unless ($_ eq "") { print "$_\n"; }
}


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top