use strict; use warnings; my @lines = ; my @records = ospf2_split_database (@lines); print @$_ for @records; sub ospf2_split_database { my @ospf2database = @_; my %data = (Router => [], Network => [], Extern => []); my $keys = join '|', keys %data; my $array; for my $line (@ospf2database) { $array = $data{$1} if $line =~ /^($keys)/; next unless defined $array; push @$array, $line; } return values %data; } __DATA__ Heading stuff that should be ignored Router: 1st router line 2nd router line this router line includes keys Network and Extern Network: 1st network record Extern: 1st extern record Extern: 2nd extern record Router: 2nd router record #### Network: 1st network record Router: 1st router line 2nd router line this router line includes keys Network and Extern Router: 2nd router record Extern: 1st extern record Extern: 2nd extern record