in reply to Reading in N lines at a time
The var name '$lines' is confusing. (I find myself asking what you are trying to tell me -- is it really more than one line each time?) I'd just call it '$line'.
Your regex does not line up with the sample data you have shown (no leading field of digits in the sample) but you'll sort that out on your own.
In the regex, you don't need to escape ':' and if you use a different regex delimiter, you don't need to escape '/'.
Also, you don't need to put parentheses around (\s+) since you are not capturing these for use later. And omitting those makes the fields you do want to capture a little spiffier to recover as explained below.
When capturing several values into a hash at once, a hash slice is a slick way to condense code. The verbose way to do this here would be:
But since we dropped the unneeded parens around the spaces, the desired fields are now just $1,$2,$3,$4,$5. That makes it easy to grab them directly by pre-loading them into another array.my %vars; @vars{'d','map','proto','uri1','uri2'} = ($1,$3,$5,$6,$8);
So this:
Becomes this. (Untested... but probably pretty close):if ($lines=~/(\d+)(\s+)(map|reverse_map)(\s+)(\w+)\:\/\/(.*?)(\s+)(\5) +\:\/\/(.*?)$/) { $vars{'d'} = $1; $vars{'map'} = $3; $vars{'proto'} = $5; $vars{'uri1'} = $6; $vars{'uri2'} = $8; } } my $result = $template->fill_in(HASH => \%vars);
I may have taken liberties with the logic of your curly-braces, but you get the idea.my @fields = qw(d map proto uri1 uri2); if (@vars{@fields} = $line=~m#(\d+)\s+(map|reverse_map)\s+(\w+)://(.*? +)\s+(\5)://(.*?)$#) { my $result = $template->fill_in(HASH => \%vars); }
HTH -- David
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Reading in N lines at a time
by Tuna (Friar) on Aug 15, 2001 at 17:21 UTC | |
by Ven'Tatsu (Deacon) on Aug 16, 2001 at 00:06 UTC | |
|
Re: Re: Reading in N lines at a time
by Tuna (Friar) on Aug 15, 2001 at 18:55 UTC | |
by rchiav (Deacon) on Aug 15, 2001 at 19:24 UTC |