tachyon has answered your specific question. But here are come general suggestions that you may find helpful. Mostly pertaining to the regex and the code lines following it.

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:

my %vars; @vars{'d','map','proto','uri1','uri2'} = ($1,$3,$5,$6,$8);
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.

So this:

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);
Becomes this. (Untested... but probably pretty close):
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); }
I may have taken liberties with the logic of your curly-braces, but you get the idea.

HTH -- David


In reply to Re: Reading in N lines at a time by dvergin
in thread Reading in N lines at a time by Tuna

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.