maddfisherman has asked for the wisdom of the Perl Monks concerning the following question:

file looks like this
<html> <head> <title>****TITLE****</title> <base href="http://south.com" /> <link rel="stylesheet" href="/stylesheets/css1.css" /> </head> <body> <table class="formatting"> <tbody> <tr> <td class="indexcell"> </td> <td class="maincell" id="top"> <h1 class="mainheading">****TITLE****</h1> ****STUFF**** <hr /> <p class="mainfooter"><a href="http://south.com/home.html#top" target= +"_top">south.com</a></p> <p class="mainfooter">Revised:</p> </td> </tr> </tbody> </table> </body> </html>
I want to separate the file into an array with the "****keywords****" as the break between elements how should i do this. So far this is what i did but it doesnt work why?
open(TEMPLATE, "<template.html") || die $!; my @xtemplate; @xtemplate = <TEMPLATE>; close (TEMPLATE); my $xtemplate; $xtemplate = join("", @xtemplate); my @template; @template = $xtemplate =~ /^(.*?)\*{4}filename.ext\*{4}/s; push (@template, $xtemplate =~ /\*{4}filename.ext\*{4}(.*?)\*{4}title\ +*{4}/s); push (@template, $xtemplate =~ /\*{4}title\*{4}(.*?)\*{4}headings\*{4} +/s); push (@template, $xtemplate =~ /\*{4}headings\*{4}(.*?)/s);
maybe i should use split or the $/ instead of regex??????

Replies are listed 'Best First'.
Re: regex vs split
by Zaxo (Archbishop) on Aug 23, 2001 at 08:34 UTC

    Unless this is a learning exercise, I'd suggest HTML::Template or Template Tookit for this kind of thing.

    As an exercise, here's one approach. Start with a hash of keywords=>replacements:

    # this is a complete fragment but not an application. It should # start with 'use strict; use warnings' and proceed to set up your # needed support for file names, variables to interpolate in the # substitutions, etc. my %repples = { 'TITLE' => 'An Exercise', 'STUFF' => 'Contents of my Pack', }; my $keyalt = join '|', keys %repples; # 'STUFF|TITLE' *may* be the value of $keyalt, the order might differ open TEMPLATE, "< template.html" or die $!; open FINISHED, "> finished.html" or die $!; while (<TEMPLATE>) { s/\*{4}($keyalt)\*{4}/$repples{$1}/g; print FINISHED; } close(FINISHED) or die $!; close(TEMPLATE) or die $!;

    Constructing the alternation $keyalt from the substitution keys allowa the intruduction of new keys with a minimum of fuss.

    I guess the answer to the title question is "It depends." It depends on what your goal is. The local $/ = '****'; while (<TEMPLATE>) { chomp; ...} approach and the split on a fixed string approach both looked feasable and natural when it seemed to be an odd database format we were talking about. As it turns out, there is no need for all that manipulation.

    After Compline,
    Zaxo

      So, maddfisherman, if you have free time to develop something about HTML templates, you may want to peek into HTML::Template mail list. They were contemplating adding recursive self-include as a new feature. So you can play your heart out, and also contribute to our perl community. Have fun!

      pmas
      To make errors is human. But to make million errors per second, you need a computer.

Re: regex vs split
by jryan (Vicar) on Aug 23, 2001 at 07:51 UTC
    Madfisherman, i think you are trying to create a template program... I think this is closer to what you are looking for.
    open(TEMPLATE, "template.html") or die $!; my @xtemplate = <TEMPLATE>; close (TEMPLATE); my $xtemplate = join("", @xtemplate); # get these values from cgi or whereever $replace1 ="meep"; $replace2 = "narf"; # your markers @markers = ("TITLE", "STUFF"); # your replacement values, arrayisized (ITS A WORD I TELL YOU!) @replace = ($replace1, $replace2); for ($i=0; $i<@markers; $i++) { eval ("\$xtemplate =~ s/\\*\\*\\*\\*$markers[$i]\\*\\*\\*\\*/$repl +ace[$i]/g"); } # your formatted result print $xtemplate;
      You're a C programmer, aren't you?
      open(TEMPLATE, "template.html") or die $!; my $xtemplate = join '', <TEMPLATE>; close (TEMPLATE); # your markers my @markers = qw(TITLE STUFF); # your replacement values, arrayisized (ITS A WORD I TELL YOU!) my @replace = qw(meep narf); $xtemplate =~ s/\*{4}$markers[$_]\*{4}/$replace[$_]/g for (0 .. $#markers); # your formatted result print $xtemplate;

      ------
      /me wants to be the brightest bulb in the chandelier!

      Vote paco for President!