seismofish has asked for the wisdom of the Perl Monks concerning the following question:
I frequently have to scan files for lines matching various patterns and extract data from those lines. I use code like
but I hate constructs like ($1,$2,$3,$4,$5,$6,$7)my $re_dn = qr{^(\w+) (\d+) (\d{2}:\d{2}:\d{2}) .*: eth(\d): link down +}; my $re_dn = qr{^(\w+) (\d+) (\d{2}:\d{2}:\d{2}) .*: eth(\d): link up, +(\d+)Mbps, ([^,]+), lpa (\w+)}; while(<>) { if ( /$qr_dn/ ) { my( $mon, $day, $time, $interface ) = ($1,$2,$3,$4); # process next; } if ( /$qr_up/ ) { my( $mon, $day, $time, $interface, $rate, $duplex, $lpa ) += ($1,$2,$3,$4,$5,$6,$7); # process next; } }
I'd love to know a more elegant idiom. Will someone enlighten me?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: scan, match and extract
by Fletch (Bishop) on May 19, 2022 at 11:26 UTC | |
|
Re: scan, match and extract
by hippo (Archbishop) on May 19, 2022 at 13:05 UTC | |
by Marshall (Canon) on May 20, 2022 at 05:30 UTC | |
|
Re: scan, match and extract
by LanX (Saint) on May 19, 2022 at 11:46 UTC | |
|
Re: scan, match and extract
by tybalt89 (Monsignor) on May 19, 2022 at 14:35 UTC | |
|
Re: scan, match and extract
by kcott (Archbishop) on May 19, 2022 at 14:43 UTC | |
|
Re: scan, match and extract
by BillKSmith (Monsignor) on May 19, 2022 at 20:48 UTC |