in reply to swapping PIPE for comma in CSV file

Using split with a limit along with reverse seems to be a good way to isolate the fields in this particular case. However, I would agree with some other Monks recommendations to use a module in most circumstances. I have cut down the length of the fields to avoid too much line wrapping.

use strict; use warnings; while ( <DATA> ) { my @f012 = split m{,}, $_, 4; my $rest = pop @f012; my @f7654 = map { my $r = reverse } split m{,}, reverse($rest), 5; my $f3 = pop @f7654; print join q{|}, @f012, $f3, reverse @f7654; } __END__ "13F2","E3C9","05E5","J0180794.JPG",32768,3290,"WIN","" "D05C","2EF2","5E8D","WabIab, and more.bor",4760,4616,"WIN","" "6DAC","B87B","8D89","fpSDt,Finder,Link.gif",1161,2988,"Solaris","" "4DE1","BC7A","2D72","cmnres,pdb.dll",76800,1550,"WIN",""

The output.

"13F2"|"E3C9"|"05E5"|"J0180794.JPG"|32768|3290|"WIN"|"" "D05C"|"2EF2"|"5E8D"|"WabIab, and more.bor"|4760|4616|"WIN"|"" "6DAC"|"B87B"|"8D89"|"fpSDt,Finder,Link.gif"|1161|2988|"Solaris"|"" "4DE1"|"BC7A"|"2D72"|"cmnres,pdb.dll"|76800|1550|"WIN"|""

I hope this is of interest.

Cheers,

JohnGG

Update: There's no need for the lexical in the map, $_ will do, and there's no need to pop off $f3 as we are not doing anything with it. Code becomes

while ( <DATA> ) { my @f012 = split m{,}, $_, 4; my $rest = pop @f012; my @f76543 = map { $_ = reverse } split m{,}, reverse($rest), 5; print join q{|}, @f012, reverse @f76543; }

Update 2: Using scalars for the first split means the pop can be avoided and incorporate all the rest in the print statement. Code becomes

while ( <DATA> ) { my ($f0, $f1, $f2, $rest) = split m{,}, $_, 4; print join q{|}, $f0, $f1, $f2, reverse map { $_ = reverse } split m{,}, reverse($rest), 5; }