You often need to do something to a line depending on what is on the next line. One way to go is to store a line, move to the next line, decide what to do and then store the current line as appropriate.
#! /usr/perl/bin
use warnings;
use strict;
my $skip = 0;
my $last_line = <DATA>;
chomp($last_line);
while (my $current_line = <DATA>){
chomp($current_line);
if ($current_line =~ /^\d{4}-\d{2}-\d{2}/){
if (not $skip){
print $last_line, qq{\n};
}
else{
$skip = 0;
}
$last_line = $current_line;
}
else{
print $last_line, qq{ }, $current_line, qq{\n};
$skip = 1;
}
}
print $last_line, qq{\n};
__DATA__
2013-01-01 user1 user2 chat chat
2013-01-02 user3 user4 more chat more chat
2013-01-03 user5 user6 nazi nazi
chat chat nazi
2013-01-04 user 7 user8 see you in the pub
2013-01-01 user1 user2 chat chat
2013-01-02 user3 user4 more chat more chat
2013-01-03 user5 user6 nazi nazi chat chat nazi
2013-01-04 user 7 user8 see you in the pub
Boy, am I rusty. It hurt my head getting that to compile and run. :-)
Updated: Reset the $skip flag.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.