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

I have a site I built and have come near the end of its construction but there's something I can't figure out how to do. Basically I need the main script to open my page's html frontend and search for a string then append a link to another page, so all the links can be organized neatly/alphabetically on the page. So far I've setup a number comments/reference points on my html page for the script to look for

<!a> <!b> <!c> <!d>

and so on. Also in my script I have

my $alphcity = substr($city, 0, 1);
To get the first letter of the input string which could easily be plugged into  "<!$alphacity>". I just need to know how to tell the script to look for that and append to the following line in the html file. Any help would be awesome.

Thanks
B

Replies are listed 'Best First'.
Re: search for string append in file
by oko1 (Deacon) on Dec 25, 2010 at 05:15 UTC

    Rather than reinventing the wheel (and guaranteeing that you'll eventually run into a problem), why not just use a template? That's what they were created for. E.g.:

    #!/usr/bin/perl -w use strict; use HTML::Template; $|++; my @cities = ( { LABEL => "A", CITY => "Milwaukee" }, { LABEL => "B", CITY => "Milpitas" }, { LABEL => "C", CITY => "Millston" }, { LABEL => "D", CITY => "Milbrook" }, { LABEL => "E", CITY => "Millboro" } ); my $t = HTML::Template->new(filehandle => *DATA); $t->param(CITIES => \@cities); print $t->output; __END__ Content-Type: text/html <html> <head><title>Cities</title></head> <body> <TMPL_LOOP NAME="CITIES"> <p> Label: <TMPL_VAR NAME="LABEL"><br> City: <TMPL_VAR NAME="CITY"> </p> </TMPL_LOOP> </body> </html>

    Output:

    Content-Type: text/html <html> <head><title>Cities</title></head> <body> <p> Label: A<br> City: Milwaukee </p> <p> Label: B<br> City: Milpitas </p> <p> Label: C<br> City: Millston </p> <p> Label: D<br> City: Milbrook </p> <p> Label: E<br> City: Millboro </p> </body> </html>

    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf

      Appreciate the help everyone, got me looking in the right direction. The code that worked for me is this.

      open (IN, "+<site.html"); my @file = <IN>; seek IN,0,0; foreach $file (@file){ $file =~ s/$find/$dir/g; print IN $file; } close IN;

      Thanks again everyone

Re: search for string append in file
by Anonymous Monk on Dec 25, 2010 at 01:40 UTC
    Um, thats kind of confusing, couldn't you just repeat what you're already doing?

      Sorry trying to be as concise as possible without revealing the function of the code and such, what with nondisclosure agreements and such. Basically I just need the script to search a file for a word and append text to the following line in said file.