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

The code that I am trying to do is to look for duplicated tag entries,
like - <?--1-1-->text, html file here</?--1-1-->, that already exist, so let's
delete it and print the new one.
I've tried and it can't find the right match and a duplicated gets printed to end of the file. That's where I am stuck and frustrated
Can someone help.
Thank you very much!

my $filename="test.txt"; my $template_data = "unsecure/".$filename; open(DATA_IN, ">>$template_data") || print "Can't open output file1: $ +template_data\n"; #binmode DATA_IN; undef $/; $_ = <DATA_IN>; if($_=~/<\?--([^-]*)-([^-]*)-->(.*?)<\/\?--\1-\2-->/sg) { }


Here is a piece of the text file

<?--1-1--> Location 2 </?--1-1--> <?--1-2--> Location 2 </?--1-2--> <?--3-1--> Location 2 </?--3-1--> <?--4-1--> Location 2 </?--4-1--> <?--4-2--> Location 2 </?--4-2-->

Replies are listed 'Best First'.
Re: Finding item in a text file
by Aristotle (Chancellor) on Oct 18, 2002 at 20:39 UTC

    I am not exactly sure quite what it is you're trying to do. It would help if you also explained what the output is supposed to look like.

    But I think you are asking "how do I do X with Y?" when what you really want to know is "how do I do Y?". Is this meant to be a templating system for a CGI script? If so, there are oodles of already written modules for that purpose available on CPAN to suit just about any taste and purpose. Try to see if one of them fits your bill before you start coding such a thing. perrin's templating system roundup at Perl.com should be a suitable guide for your decision.

    Makeshifts last the longest.

      The program will generate an entry on the text file, something like:
      <?--1-1--> Something Here or html file. </?--1-1-->

      See this begging tag "<?--1-1-->" and end tag" </?--1-1-->" already exist on the text file.
      I want the program to check if there is a a entry like that in the text file, if it find it
      delete it and print the new one, I hope I have explaing it better, thanks again!!
        I see no place these numbers inside the tags are coming from in your script. Where do you use them in your code? I think you need to post at least some of the code in that if() block to make your intent clearer.

        Makeshifts last the longest.

Re: Finding item in a text file
by graff (Chancellor) on Oct 18, 2002 at 23:40 UTC
    Apart from figuring out what your intention is, you do seem to be having a problem with the use of file operations. These lines in your code are contradictory:
    open(DATA_IN, ">>$template_data") || print ... ... $_ = <DATA_IN>; # No, you can't do this on an output file ...

    If you want to read an existing file, make some changes to the contents, and write the modified content back to the original file, there are a few ways to do this. The "best" way may depend on how big the file is, and what sorts of changes you need to make to the contents. If the files are not huge (i.e. not multi-megabyte), and the changes might span multiple lines of text, then here's a good way:

    my $filename="test.txt"; my $template_data = "unsecure/".$filename; $/ = undef; open( DATA_IN, $template_data ) or die "$!\n"; $_ = <DATA_IN>; # read the whole file into $_; close DATA_IN; # now check for patterns in $_ that need to change, # and change them. I'm not clear yet on this part # of the problem... but when this is done, finish with: open( DATA_OUT, ">$template_data.new" ) or die "$!\n"; print DATA_OUT; close DATA_OUT; __END__

    Note that I used a different name for the output file -- this would be a good strategy for you until you have made sure that the central part of the script is doing what you want, and not corrupting the data. (Of course, you could have it over-write the input file, and just make sure you keep a separate copy of the original input, so that it will be easy to replace the input with a fresh copy each time you try to debug the script.)