my @regex = qw( Desription Data ); # regexen to match my %rhash = (); my $which = 0; # which regexp to look for while (<>) { /^$regex[$which]:/ && do { # found one of them if ($which == 0 ) { # if it was the first one .. # process Description and Data arrays (if any) process_arrays( @rhash{ @regex } ); # reinitialize by associating an anon array with each regex @rhash{ @regex } = ( [] ) x @regex; } $which = 0 if (++$which >= @regex); next; } push @{$rhash[$which]}, $_; } # Since the last Data block won't be terminated with a # Description line, need to cleanup here process_arrays( @rhash{ @regex } ); # ... sub process_arrays { my ( $arrayref1, $arrayref2 ) = @_; # return immediately if both arrays are empty return unless $arrayref1 && @$arrayref1 && $arrayref2 && @$arrayref2; # ... }