my %largerHash; # Place to accumulate interesting records my %elements; # Place to accumulate data into records while () { if (/^(yabba|dabba|doo)\s+(.*)/) { # We only care about some of the fields $elements{$1} = $2; } elsif (/End of record ID:\s+(.*)/) { if ($1 =~ /foo/) { # Interesting record (starts with foo) so, store it $largerHash{$1} = %elements; } # Since we found end of record, clear our workspace %elements={}; } } __DATA__ Record 1 scooby 7 dooby 8 yabba Fred dabba Wilma End of record ID: cupcake Record 2 doo not fold spindle staple or mutilate dabba Barney yabba Dino End of record ID: foobar