Minor niggle: bichonfrise74: At line 5, didn't you mean "...and print "$1/" while...?

This scales nicely (as do the other replies above) for a source file (which OP calls a "result file") which includes only elements of a single data set. But IMO, the actual problem is likely under-spec'ed.

Yes, I may be overthinking this but I can't help but wonder if we're giving the "X" answers where what OP is seeking may be the "Y" answers, but isn't getting them because the problem is less than fully stated.

Here's my train of thought: it seems to me that the phrase "result file" may imply that it contains sets of (previously selected) data from multiple sources (multiple rows of a DB table, for example) in which case one might expect that there will be a delimiter - - express or implied - - between the sets.

OP's ellipsis beg the question.

Suppose the source looks like this:

__DATA__ line1=10 line2=30 line3= I am a monk ine4=false line5=20 line6=42 line7= But I haven't read perlretut and I should have! line8=true line8=34 line9=67 line10=Coder line11=false

Is this one set of 12 items or three sets of 4 items each. And what if the data continues with an inconsistent set such as:

line12=00 line2=11 line3= line4=questionable

Hence, yskmonk, you may wish to enlighten us with details. Did one of the fine answers above solve your problem or does your question need refinement/more specifics?

Update: If the original question (data has since undergone a major transformation, which is still insufficiently particular) relates to sets of data, each comprising 4 lines, then one answer might be:

my @outfile = ( "line1=10", "line2=30", "line3= I am a monk", "line4=false", "line5=20", "line6=42", "line7= But I haven't read perlretut and I should have!", "line8=true", "line8=34", "line9=67", "line10=Coder", "line11=false", ); my @quads; while (@outfile) { my $line; for (1..4) { # presumption: each data set is made up +of 4 consecutive lines * my $line_from_outfile = shift (@outfile); $line_from_outfile =~ /line\d+=(.*)/; $line .= $1 . "/" ; } push @quads, $line; $line = ""; } for my $quad(@quads) { print $quad . "\n"; # to console; writing to file "final.txt" is +left as an exercise for OP }

* Season Line 19 to taste.


In reply to Re^2: how to delete part of the line? by ww
in thread how to delete part of the line? by yskmonk

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.