I am sure this was working so what's wrong now - help needed

J

John

Hi,

I'm using this from http://www.perldoc.com/perl5.004_05/pod/perlfaq5.html to
do a string substitution in a HTML file:

$old = $file;
$new = "$file.tmp.$$";
$bak = "$file.bak";

open(OLD, "< $old") or die "can't open $old: $!";
open(NEW, "> $new") or die "can't open $new: $!";

# Correct typos, preserving case
while (<OLD>) {
...
###########################The below concerns me
if ($_ =~ "A HREF=") {
my $link = $_;
$link =~ s/href="([^"]+)"/my
$x=$1;$x=~s#One#Two#g;'href="'.$x.'"'/ei;
}
(print NEW $_);
}
###########################The above concerns me

close(OLD) or die "can't close $old: $!";
close(NEW) or die "can't close $new: $!";

rename($old, $bak) or die "can't rename $old to $bak: $!";
rename($new, $old) or die "can't rename $new to $old: $!";


I'm expecting the contents of the file to change from:
<A HREF="One/One/one/One.html"> One link
to:
<A HREF="Two/Two/one/Two.html"> One link
but it still reads:
<A HREF="One/One/one/One.html"> One link

Any help will be greatly appreciated...
 
J

John

Peter Hickman said:
The i at the end of the line says 'this is case insensitive', you should
remove it.

Peter,

The /i does not seem to play a part here.
The problem is with writing the output to the file [does not work] as
opposed to the screen [which works if tried].
 
P

Peter Hickman

John said:
The i at the end of the line says 'this is case insensitive', you should
remove it.


Peter,

The /i does not seem to play a part here.
The problem is with writing the output to the file [does not work] as
opposed to the screen [which works if tried].

Ok I see what you mean.

###########################The below concerns me
if ($_ =~ "A HREF=") {
my $link = $_;
$link =~ s/href="([^"]+)"/my
$x=$1;$x=~s#One#Two#g;'href="'.$x.'"'/ei;
}
(print NEW $_);
}
###########################The above concerns me

Note that you started with the line in $_, which you copied into $link. You
then modified $link but wrote out $_.

Try

if ($_ =~ "A HREF=") {
$link =~ s/href="([^"]+)"/my $x=$1;$x=~s#One#Two#g;'href="'.$x.'"'/ei;
print NEW $link;
} else {
print NEW $_;
}
 
A

Anno Siegel

John said:
Peter Hickman said:
The i at the end of the line says 'this is case insensitive', you should
remove it.

Peter,

The /i does not seem to play a part here.
The problem is with writing the output to the file [does not work] as
opposed to the screen [which works if tried].

One problem with the original code is that you never write back the
changed value. The relevant code again:

if ($_ =~ "A HREF=") {
my $link = $_;
$link =~ s/href="([^"]+)"/my $x=$1;$x=~s#One#Two#g;'href="'.$x.'"'/ei;
}
(print NEW $_);

You change the text in $link, but you write back $_. Apply the substitution
to $_ directly. Mind, I didn't check if the substitution does what it
should do, but whatever it does, it won't end up in the file.

Anno
 
J

John

Peter Hickman said:
John said:
John wrote:

$x=$1;$x=~s#One#Two#g;'href="'.$x.'"'/ei;

The i at the end of the line says 'this is case insensitive', you should
remove it.


Peter,

The /i does not seem to play a part here.
The problem is with writing the output to the file [does not work] as
opposed to the screen [which works if tried].

Ok I see what you mean.

###########################The below concerns me
if ($_ =~ "A HREF=") {
my $link = $_;
$link =~ s/href="([^"]+)"/my
$x=$1;$x=~s#One#Two#g;'href="'.$x.'"'/ei;
}
(print NEW $_);
}
###########################The above concerns me

Note that you started with the line in $_, which you copied into $link. You
then modified $link but wrote out $_.

Try

if ($_ =~ "A HREF=") {
$link =~ s/href="([^"]+)"/my $x=$1;$x=~s#One#Two#g;'href="'.$x.'"'/ei;
print NEW $link;
} else {
print NEW $_;
}

Yeah! Thanks heaps. I was looking at it for ages and tried fiddling with the
output but my lines in the new file were doubling up. Somehow I was
finishing up with one line which was the original and another which was the
replaced one. Thanks again Peter :c)
 
J

John

Anno Siegel said:
John said:
Peter Hickman said:
John wrote:
$x=$1;$x=~s#One#Two#g;'href="'.$x.'"'/ei;

The i at the end of the line says 'this is case insensitive', you should
remove it.

Peter,

The /i does not seem to play a part here.
The problem is with writing the output to the file [does not work] as
opposed to the screen [which works if tried].

One problem with the original code is that you never write back the
changed value. The relevant code again:

if ($_ =~ "A HREF=") {
my $link = $_;
$link =~ s/href="([^"]+)"/my $x=$1;$x=~s#One#Two#g;'href="'.$x.'"'/ei;
}
(print NEW $_);

You change the text in $link, but you write back $_. Apply the substitution
to $_ directly. Mind, I didn't check if the substitution does what it
should do, but whatever it does, it won't end up in the file.

Anno

Thanks as well Anno. Sometimes those obvious things are the most difficult
to spot.
I amended the script to reflect Peter's suggestion and all is well now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top