Yes, but then, mine worked. :) Just kidding.

You're just pushing each line arbitrarily onto @recs--you need to group them in sets of 5, because that's what the original file looked like. Plus why are you using foreach in the original reading-in-the-file loop? That

foreach my $line (<FH>)
reads the entire file into a temp array--not a horrible thing in many cases, but still, if we can process on a line by line basis, we may as well do so. :)

I like your use of split quite a lot, though.

So, combining your ideas and mine:

use strict; open FH, "foo" or die "Can't open: $!"; my @recs; while (<FH>) { chomp; push @{$recs[int(($.-1) / 5)]}, split /,?\s/; } close FH or die "Can't close: $!"; open FH, ">foo.new" or die "Can't open: $!"; print FH map join('|', @$_) . "\n", @recs; close FH;
Notice I took the array slice out--I think the op wanted everything in the array. If not, though, he/she should stick it back in, just
@$ref[0..4]
instead of
@$ref
And I'm now using map, just cause map is great.

In reply to RE: RE: Re: Simple Text Conversion by btrott
in thread Simple Text Conversion by Anonymous Monk

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.