my %entries; # key=calendar name; value=array(ref) of dates. my $calendar; my @dates; sub finish_pending_entry { if ( defined $calendar ) { $entries{$calendar} = [ @dates ]; } # re-init the state data: $calendar = undef; @dates = (); } while (<>) { chomp; if ( /calendar: (.*)/ ) { finish_pending_entry(); # possibly none, if this is the first line. $calendar = $1; } else { push @dates, $_; } } finish_pending_entry(); # possibly none, if file was empty.