PDA

View Full Version : Replacing Text


crca
11-14-2002, 09:26 PM
I am finishing my serch engine.

I need to ask this:

I have text on the page that says "*YT*" I would like to replace this with "NOT FOUND" but how do I do It?

I sofar have:

open (FILE, "dafile");
@LINES=<FILE>;
close(FILE);
$#SIZE=@LINES;

for ($i = 0; $i < $#SIZE; $i++) {
@LINES[$i] =~ s/(\*YT\*)//isg;
print "<BR>@LINES[$i]";
}


But that will replace it with nothing!

Any help will do

chrisvmarle
11-15-2002, 07:55 AM
Change:
@LINES[$i] =~ s/(\*YT\*)//isg;
To:
@LINES[$i] =~ s/(\*YT\*)/Not Found/isg;

crca
11-15-2002, 08:36 AM
Thanks for that

Mouldy_Goat
11-17-2002, 06:20 PM
Hi crca,

Just noticed,

@LINES[$i] should be $LINES[$i], since you're not taking a list out of @LINES - just a single scalar variable. If you were extracting multiple lines to get a list you would do something like:
@LINES[1..4] to get the 2nd, 3rd, 4th and 5th elements from the array.

Also this:$#SIZE=@LINES;
for ($i = 0; $i < $#SIZE; $i++) { is fairly inefficient, since it involves copying the size of @LINES into an array called $#SIZE which is created with that many elements.

Why not just do:
for ($i = 0; $i < scalar @LINES; $i++) { instead..?

crca
11-19-2002, 08:12 AM
that has actually made the script load faster!