while ($content =~ /(e)(x)(p)r/g) { $hash{$1}={ something=>$2, another=>$3 } } #### my $c=0; my @subkeys = ('', 'something', 'another', 'third'); my $k; for ( split(/(?:box)?\n\s*/) ) { if ($c) { ${$hash{$k}}}{$subkeys[$c]}=$_; } else {$k = $_ } $c = ($c + 1)%4; }