Home: Perl Programming Help: Intermediate:
calendar records



cycleops
Novice

Dec 3, 2011, 12:18 PM


Views: 2505
calendar records

Hi,
I'm trying to separate text into records. The text comes from calendar entries showing the day number followed by the entry for that day (if any). An example of the first 4 days of a month is:

1
blah, blah, blah
over 2 lines

2
3

2nd something or other starting at
8 p.m.

sometimes more than one line for the entry
4

I've tried several approaches with regex and split, but can't seem to capture the date and the entry for the date.
Any help/advice is appreciated.
Thanks in advance.


BillKSmith
Veteran

Dec 4, 2011, 6:15 AM


Views: 2492
Re: [cycleops] calendar records

This should get you started.


Code
use strict; 
use warnings;
my @entry;
my $new_day;
while (my $line = <DATA>) {
if ( $line =~ /^([1-9]|[12]\d|3[01])\s*$/ ) {
$new_day = $1;
next;
}
die 'internal error' if !$new_day;
$entry[$new_day] .= $line;
}
for my $day (1..4) {
print $day, "\n", $entry[$day], "\n";
}
__DATA__
1
blah, blah, blah
over 2 lines

2
3

2nd something or other starting at
8 p.m.

sometimes more than one line for the entry
4

Good Luck,
Bill


cycleops
Novice

Jan 30, 2012, 8:04 AM


Views: 2260
Re: [BillKSmith] calendar records

That worked.
Thanks very much, Bill.