Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: Reading in N lines at a time

by Tuna (Friar)
on Aug 15, 2001 at 18:55 UTC ( [id://105061]=note: print w/replies, xml ) Need Help??


in reply to Re: Reading in N lines at a time
in thread Reading in N lines at a time

hmmm. Something is wrong that I can't figure out:

#!/usr/bin/perl -w use strict; use Text::Template; my %vars; my $line; my @fields; my $result; my $file = "/home/trixee/20010814/remap.tmpl"; my $cfg = "/home/trixee/20010814/remap.config"; my $template = new Text::Template (SOURCE => $file) or die "Couldn't construct template: $Text::Template::ERROR"; open FH, "$cfg" or die "$!\n"; while ($line = <DATA>) { chomp $line; next if ($line =~ /^\#/); @fields = qw(map proto uri1 uri2); if ( @vars{@fields} = $line=~m!(map|reverse_map)\s+(\w+)://(.*?)\s ++\2://(.*?)$! ) { $result = $template->fill_in(HASH => \%vars); } if ($result) { print "$result\n"; } else { die "Couldn't fill in template: $Text::Template::ERROR" } } __DATA__ # Automatic mapping tables generated from Magma map http://www.digitalcity.com http://origin-www.digitalcity.com map tunnel://www.digitalcity.com tunnel://origin-www.digitalcity.com map http://10.0.2.1 http://origin-www.digitalcity.com map tunnel://10.0.2.1 tunnel://origin-www.digitalcity.com reverse_map http://origin-www.digitalcity.com http://www.digitalcity.c +om reverse_map tunnel://origin-www.digitalcity.com tunnel://www.digitalci +ty.com Template: {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri2} {$proto}://{$uri1} {$map} {$proto}://{$uri2} {$proto}://{$uri1}

prints

map http://www.digitalcity.com http://origin-www.digitalcity.com map http://www.digitalcity.com http://origin-www.digitalcity.com map http://www.digitalcity.com http://origin-www.digitalcity.com map http://www.digitalcity.com http://origin-www.digitalcity.com map http://origin-www.digitalcity.com http://www.digitalcity.com map http://origin-www.digitalcity.com http://www.digitalcity.com map tunnel://www.digitalcity.com tunnel://origin-www.digitalcity.com map tunnel://www.digitalcity.com tunnel://origin-www.digitalcity.com map tunnel://www.digitalcity.com tunnel://origin-www.digitalcity.com map tunnel://www.digitalcity.com tunnel://origin-www.digitalcity.com map tunnel://origin-www.digitalcity.com tunnel://www.digitalcity.com map tunnel://origin-www.digitalcity.com tunnel://www.digitalcity.com map http://10.0.2.1 http://origin-www.digitalcity.com map http://10.0.2.1 http://origin-www.digitalcity.com map http://10.0.2.1 http://origin-www.digitalcity.com map http://10.0.2.1 http://origin-www.digitalcity.com map http://origin-www.digitalcity.com http://10.0.2.1 map http://origin-www.digitalcity.com http://10.0.2.1 map tunnel://10.0.2.1 tunnel://origin-www.digitalcity.com map tunnel://10.0.2.1 tunnel://origin-www.digitalcity.com map tunnel://10.0.2.1 tunnel://origin-www.digitalcity.com map tunnel://10.0.2.1 tunnel://origin-www.digitalcity.com map tunnel://origin-www.digitalcity.com tunnel://10.0.2.1 map tunnel://origin-www.digitalcity.com tunnel://10.0.2.1 reverse_map http://origin-www.digitalcity.com http://www.digitalcity.c +om reverse_map http://origin-www.digitalcity.com http://www.digitalcity.c +om reverse_map http://origin-www.digitalcity.com http://www.digitalcity.c +om reverse_map http://origin-www.digitalcity.com http://www.digitalcity.c +om reverse_map http://www.digitalcity.com http://origin-www.digitalcity.c +om reverse_map http://www.digitalcity.com http://origin-www.digitalcity.c +om reverse_map tunnel://origin-www.digitalcity.com tunnel://www.digitalci +ty.com reverse_map tunnel://origin-www.digitalcity.com tunnel://www.digitalci +ty.com reverse_map tunnel://origin-www.digitalcity.com tunnel://www.digitalci +ty.com reverse_map tunnel://origin-www.digitalcity.com tunnel://www.digitalci +ty.com reverse_map tunnel://www.digitalcity.com tunnel://origin-www.digitalci +ty.com reverse_map tunnel://www.digitalcity.com tunnel://origin-www.digitalci +ty.com

Why?

Replies are listed 'Best First'.
Re: Re: Re: Reading in N lines at a time
by rchiav (Deacon) on Aug 15, 2001 at 19:24 UTC
    From your description you gave in the CB, you're looking for it to print 6 lines according to the layout of the template.

    It is doing that, bu not as I assume you wish. I'm not all that knowledgeable about Text::Template, but this is what I see happening. You're defining your fields..

    @fields = qw(map proto uri1 uri2);
    ..then you are parsing and passing your data..
    if ( @vars{@fields} = $line=~m!(map|reverse_map)\s+(\w+)://(.*?)\s ++\2://(.*?)$! ) { $result = $template->fill_in(HASH => \%vars); }
    ...and your template looks like this..
    {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri2} {$proto}://{$uri1} {$map} {$proto}://{$uri2} {$proto}://{$uri1}
    Now, you read one(1) line, parsed it and passed it to fill in the template. For the first line, your values are going to look like this..
    $map => map $proto =>http $uri1 => www.digitalcity.com $uri2 => origin-www.digitalcity.com
    When you pass it to $template->fill_in(), it fills in the whole template. So on the line, the template is filled in completely (all six lines) and the results are the first 6 that are printed. Rinse, wash and repeat for the rest of the lines you're parsing.

    I assume that this isn't the effect that you want. But to fix it you're going to have to take a different approach. Namely, reading in 6 lines and using different variables on each line to designate which values go where.

    There might be a better way using Text::Template but I'm really not all that familiar with the module.

    Hope this helps..
    Rich

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://105061]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-05-21 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found