in reply to Need advice on how to break foreach parsing loop into functions
Less importantly, you are using a leading ampersand in the sub calls "&parse_ethernet($1);" and "&parse_gigabit($1);". They are unnecessary and you shouldn't use them unless you know what they do and want tthe effect. Here, you don't.
As for your parsing problem, you can set the input separator to "!\n" when you read the file. Then you will have one complete chunk of input to give to any of the subs parse_* or whatever. Note that these must now expect a multiline string containing all the information in the current chunk.
Here is a sketch:
Anno$/ = "!\n"; while ( <DATA> ) { chomp; if ( /^interface (.*)/ ) { if ( $1 eq 'Ethernet' ) { parse_ethernet( $_); } elsif ( $1 eq 'Gigabit' ) { parse_gigabit( $_); } } elsif ( /^system (.*)/ ) { # blah... } } sub parse_gigabit { my $chunk = shift; $chunk =~ tr/\n/ /; print "GIGABIT: $chunk\n"; } sub parse_ethernet { my $chunk = shift; $chunk =~ tr/\n/ /; print "ETHERNET: $chunk\n"; } __DATA__ ! interface Ethernet blah... blah... ! interface Gigabit blah... blah... ! interface Ethernet blah... !
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Need advice on how to break foreach parsing loop into functions
by Prof Vince (Friar) on Sep 16, 2007 at 08:36 UTC | |
by Anonymous Monk on Sep 16, 2007 at 17:22 UTC |