LPM: regex question --

John Soward soward at uky.edu
Tue Dec 14 11:30:56 CST 1999


"0x29A [B.Vandgrift]" wrote:
> 
> This one's a little beyond my ken.  I'd appreciate some help.
> 
> I need to replace something like:
> 
> "one","two","three","four,five","six"
> 
> with
> 
> "one";"two";"three";"four,five";"six"
> 
> What's the move?
> 

rather than fight with a regex set, I'd use the Text::CSV module as
such:

#!/usr/bin/perl

#Use cool CSV module from CPAN
require Text::CSV;

#instantiate new CSV parser object
$csv = Text::CSV->new;

while ($line=<>) {
        $column = '';
        $newline= '';
        if ($csv->parse($line)) {
                @field = $csv->fields;
                $x=0;
                for $column (@field) {
                        print $column;
                        $x++;
                        if ($x > $#field) {
                                print "\n";
                        } else {
                                print ";";
                        }
                }
        }
}         

of course you could also accomplish the same in much fewer code-lines
using various other perl techniques, I chose an explicit while and if
set of contructs for simplicity of the
illustration.                        
-- 
John Soward     
Lead Systems Programmer, Technical Services, University of Kentucky    
p: 606.257.2900x298  e:soward at uky.edu  w: http://neworder.cc.uky.edu/



More information about the Lexington-pm mailing list