Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Search and Replace Text File

by Anonymous Monk
on Oct 21, 2002 at 19:21 UTC ( #206907=perlquestion: print w/replies, xml ) Need Help??

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

I am looking to find a string in a text file and replace with a new value, here is the code I am having difficulty with, can anyone help find out why I can not print to the text file with the new value found? Thank you!!! Here is the test.txt
<?--1-News1--> <b>This is option News 1 of 1</b> </?--1-News1--> <?--1-Weather2of1--> <b>This is option Weather 2 of 1</b> </?--1-Weather2of1--> <?--1-Stock3of1--> <b>This is option Stock 3 of 1</b> </?--1-Stock3of1--> <?--2-Second1of2--> <b>Option Second 1 of 2</b> </?--2-Second1of2--> <?--3-Third1of3--> <b>Option Third 1 of 3</b> </?--3-Third1of3-->

Here is the program I am trying to write:

use strict; use CGI qw(:header); use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; print "content-type: text/html\n\n"; #&getdata(%in); &getdata; #$location = $in{'location'}; #$obj_name = $in{'obj_name'}; #$page = $in{'page'}; #The value on these variable are hard coded for testing #my $location=param('location'); $location="1"; #my $obj_name=param('obj_name'); $obj_name="News1"; $page="Testing New Content for Object ONE TWO"; my $filename="test.txt"; my $template_data = "unsecure/".$filename; undef $/; # Slurp mode open(DATA_IN, "$template_data") || print "Can't open output file1: $te +mplate_data\n"; #binmode DATA_IN; $_ = <DATA_IN>; #close DATA_IN; while(/<\?--([^-]*)-([^-]*)-->(.*?)<\/\?--([^-]*)-([^-]*)-->/sg){ #<?--1-News1--> #<b>This is option News 1 of 1</b> #</?--1-News1--> $a=$1;$b=$2;$c=$3;$d=$4;$e=$5; if ($a eq $location){ if($b eq $obj_name) { #$c=~s/$c/$page/ig; $c=$page; open( DATA_OUT, ">/unsecure/" ) or d +ie "$!\n"; print DATA_OUT "<?--$a-$b-->\n"; print DATA_OUT "$c\n"; print DATA_OUT "</?--$d-$e-->\n"; + } } } #print DATA_OUT; close DATA_OUT; close DATA_IN; #print "content-type: text/html\n\n"; print "<b> EDITOR</b><br><br>\n"; print "<hr>"; print "You're about to change location:&nbsp;&nbsp;&nbsp;&nbsp;<b>$loc +ation</b>"; print "<br>\n"; print "Viewing object name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>$obj +_name</b>"; print "<hr>\n"; print "<br><br><br><br>\n"; print "<b>$page</b>"; print "<br><br><br><br>";; print "<hr>"; print "<form>\n"; print " <input type=\"submit\" value=\"submit\">\n"; print "</form>\n";

I cant see where the problem is. Thanks again!!!

Replies are listed 'Best First'.
Re: Search and Replace Text File
by sauoq (Abbot) on Oct 21, 2002 at 20:17 UTC
    can anyone help find out why I can not print to the text file with the new value found?
    . . .
    open( DATA_OUT, ">/unsecure/" ) or die "$!\n";

    My guess is that you don't really want an absolute path there and that the directory /unsecure doesn't exist. Earlier in the code you used a relative path. Try removing the leading slash.

    "My two cents aren't worth a dime.";
Re: Search and Replace Text File
by earthboundmisfit (Chaplain) on Oct 21, 2002 at 19:43 UTC
    Just a quick glance at this suggests some Cargo Cultish practices, but the most glaring problem is that this script won't pass the strict pragma use strict; because you have the correct variable declarations commented out. Aside from that, try inserting the standard error var in your die statements. The way you have it, all data files must reside in the same directory the script is running. If that's indeed the case and it still won't work after making it strict compliant, try looking at your error logs to see what might be the problem.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Search and Replace Text File
by Schemer (Scribe) on Oct 21, 2002 at 20:30 UTC

    I guess the problem you are looking into solving revolves around your regexp not extracting your data as you would like.

    Maybe you should consider putting your data is a less half-assed format (yours looks like XML, but it's not even XML, so it's even worse).

    Use a simpler file format, don't reinvent XML badly. If you really need XML, use a module that will help you work with _real_ XML.

    Here is an example of what you could have used:

    Instead of: <?--1-News1--> <b>This is option News 1 of 1</b> </?--1-News1--> <?--3-Third1of3--> <b>Option Third 1 of 3</b> </?--3-Third1of3--> what about: 1,News,1,1 3,Third,1,3 Then you can do something like: ($num, $name, $index, $total) = split /,/, $data;

    Use simple data representation for simple stuff, if it get more complex look into using a database. Don't overwhelm yourself with <,>,--,/ and friends. Aim for simplicity, your problems will resolve themselves.

    And finally, if you don't know what a particular piece of code in your program does (because, say, you've done some ctrl-c and some ctrl-v), remove it for now. Later, if you have a problem and you need to put it back in, you'll understand what it does.

Re: Search and Replace Text File
by princepawn (Parson) on Oct 21, 2002 at 21:13 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://206907]
Approved by fireartist
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-09-24 16:58 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (114 votes). Check out past polls.