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; }

In reply to Re: swapping PIPE for comma in CSV file by johngg
in thread swapping PIPE for comma in CSV file by dwhite20899

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.