Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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/test.new" ) 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!!!

In reply to Search and Replace Text File by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found