Tuna has asked for the wisdom of the Perl Monks concerning the following question:
I have a file, in which I am trying to check syntax, 6 lines at a time. The file will vary in length and may contain comments and/or empty lines.
I'm using Text::Template (thanks to a few of you) to create a template of what each 6-line block should look like:
{$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}
Here's some sample data that I am working with:
map http://chat.yahoo.com http://origin-chat.yahoo.com map tunnel://chat.yahoo.com tunnel://origin-chat.yahoo.com map http://10.0.2.7 http://origin-chat.yahoo.com map tunnel://10.0.2.7 tunnel://origin-chat.yahoo.com reverse_map http://origin-chat.yahoo.com http://chat.yahoo.com reverse_map tunnel://origin-chat.yahoo.com tunnel://chat.yahoo.com map http://shop.yahoo.com http://origin-shop.yahoo.com map tunnel://shop.yahoo.com tunnel://origin-shop.yahoo.com map http://10.0.2.8 http://origin-shop.yahoo.com map tunnel://10.0.2.8 tunnel://origin-shop.yahoo.com reverse_map http://origin-shop.yahoo.com http://shop.yahoo.com reverse_map tunnel://origin-shop.yahoo.com tunnel://shop.yahoo.com
Here's the program, thus far:
#!/usr/bin/perl -w use strict; use Text::Template; my %vars; my $lines; my @lines; my $file = "/home/trixee/remap.config"; my $template = new Text::Template (TYPE => 'FILE' , SOURCE => $file) or die "Couldn't construct template: $Text::Template::ERROR"; open FH, "$file" or die "$!\n"; while ($lines = <FH>) { chomp $lines; next if ($lines =~ /^\#/); 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); if (defined $result) { print $result } else { die "Couldn't fill in template: $Text::Template::ERROR" }
Using the example data above, I would need to modify the existing code to read lines 1-6 and populate the template. Then, I would add logic to compare the populated template with lines 1-6 of the data file. If they are equal, then read in lines 7-12, and repeat the comparison logic.
Any ideas? Am I appoaching this efficiently?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reading in N lines at a time
by dvergin (Monsignor) on Aug 15, 2001 at 09:27 UTC | |
by Tuna (Friar) on Aug 15, 2001 at 17:21 UTC | |
by Ven'Tatsu (Deacon) on Aug 16, 2001 at 00:06 UTC | |
by Tuna (Friar) on Aug 15, 2001 at 18:55 UTC | |
by rchiav (Deacon) on Aug 15, 2001 at 19:24 UTC | |
|
Re: Reading in N lines at a time
by tachyon (Chancellor) on Aug 15, 2001 at 07:02 UTC | |
|
Re: Reading in N lines at a time
by Ven'Tatsu (Deacon) on Aug 16, 2001 at 12:23 UTC |