Loading ...
Sorry, an error occurred while loading the content.

[PBML] How to insert a string?

Expand Messages
  • Xiaoxiong Zhu
    Hi,there. I need your help. I am using perl to wirte a cgi scripts. I need to insert a string into a data file called data.html . Suppose a tag
    Message 1 of 2 , Aug 24 5:51 AM
    • 0 Attachment
      Hi,there. I need your help. I am using perl to wirte a cgi scripts. I
      need to insert a string into a data file called data.html . Suppose a
      tag <!--insert string here-->is embeded in data.html . I want a string
      insert after this tag. What if this data file is not exist before but
      how it can be created from a template file such as template.html?

      It is concluded that I want my scripts to :
      1.Create a new file from a template;
      2.Insert a string into somewhere in this newly created file;

      Thanks a lot.

      Xiaoxiong Zhu
      Shanghai,China


      ____________________________________________________________
      List Site: http://www.findmail.com/list/perl-beginner/
      To unsubscribe, send to perl-beginner-unsubscribe@...

      FREE group e-mail lists at http://www.findmail.com
    • Dave Dustin
      ... This quickly (and probably badly) written piece of code below will do what you want. It will check to see if data.html exists, if it does, we work with
      Message 2 of 2 , Aug 25 3:09 AM
      • 0 Attachment
        >Hi,there. I need your help. I am using perl to wirte a cgi scripts. I
        >need to insert a string into a data file called data.html . Suppose a
        >tag <!--insert string here-->is embeded in data.html. I want a string
        >insert after this tag. What if this data file is not exist before but
        >how it can be created from a template file such as template.html?

        >It is concluded that I want my scripts to :
        >1.Create a new file from a template;
        >2.Insert a string into somewhere in this newly created file;

        This quickly (and probably badly) written piece of code below will do what
        you want. It will check to see if data.html exists, if it does, we work
        with it, otherwise we work with the template file.

        It pre-loads the entire file into an array, checking each line as we go to
        see if we have a match on our search string. If we do then we replace it
        with the new string.

        The routine can be improved but I've kept it simple to demonstrate.



        #!/usr/bin/perl

        $searchstring = "<!--insert string here-->";
        $newstring = "whatever string you want to insert";
        $docpath = "/usr/httpd/htdocs/";

        if (!-e "$docpath/data.html") {
        open(FILE,"$docpath/template.html");
        } else {
        open(FILE,"$docpath/data.html");
        }

        while (<FILE>) {
        if (/$searchstring/i) {
        s/$searchstring/$newstring/oi;
        }
        push @lines,$_;
        }
        close(FILE);

        open(NEWFILE,">$docpath/data.html");
        print NEWFILE @lines;
        close(NEWFILE);



        --
        Dave Dustin

        "Forgive and Remember"

        ____________________________________________________________
        List Site: http://www.findmail.com/list/perl-beginner/
        To unsubscribe, send to perl-beginner-unsubscribe@...

        FREE group e-mail lists at http://www.findmail.com
      Your message has been successfully submitted and would be delivered to recipients shortly.