in reply to Re: Need advice on how to break foreach parsing loop into functions
in thread Need advice on how to break foreach parsing loop into functions
Edit: As anonymous said, ( ) instead of { }. Copypasta from/to Firefox is treacherous.my %parsers = ( 'Ethernet' => sub { my $chunk = shift; $chunk =~ tr/\n/ /; return "ETHERNET: $chunk"; }, 'Gigabit' => sub { my $chunk = shift; $chunk =~ tr/\n/ /; return "GIGABIT: $chunk"; } ); $/ = "!\n"; $\ = "\n"; while ( <DATA> ) { chomp; if ( /^interface (.*)/ && $1 && exists $parsers{$1} ) { print $parsers{$1}( $_ ); } elsif ( /^system (.*)/ ) { # blah... } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Need advice on how to break foreach parsing loop into functions
by Anonymous Monk on Sep 16, 2007 at 17:22 UTC |