in reply to Is there a better way to approach this?
Use a hash of arrays
my %matches; my $section; ... if ( $line =~ m/^(Router|Network|Extern)/ ) { $section = $1; } push @{ $matches{$section} }, $line;
And I'd stay clear of Switch if I were you. It can lead to some difficult to trace bugs.
update
Here's a reimplementation of your subroutine, along with example usage
use Data::Dumper; my $matches = ospf2_split_database( "no match\n", "Extern 1\n", "Network 1\n", "Stuff for Network 1a\n", "Stuff for Network 1b\n", "Stuff for Network 1c\n", "Network 2\n", "Stuff for Network 2a\n", "Stuff for Network 2b\n", "Router 1\n", "Router 2\n", "Extern 2\n", ); print Dumper $matches->{Network}; sub ospf2_split_database { my %matches; my @ospf2database = @_; my $section; for my $line ( @ospf2database ) { chomp $line; if ( $line =~ m/^(Router|Network|Extern)/ ) { $section = $1 } push @{ $matches{$section} }, $line if $section; } return \%matches; }
Update^2: Now meets the clarified spec
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is there a better way to approach this?
by ewhitt (Scribe) on Mar 29, 2008 at 09:51 UTC | |
by oko1 (Deacon) on Mar 30, 2008 at 03:44 UTC |