use strict; use warnings; use Data::Dumper; my @sections; while () { chomp; if (/===Comments===/../^[^=]/) { if (/===Comments===/) { push @sections,[] } else { push @{$sections[-1]}, $_ if $_ } } } print Dumper(\@sections);