Link to home
Start Free TrialLog in
Avatar of spoma
spoma

asked on

Search and Replace of text with array

I am reading a portion of a file into an array.  Then I print out portions of the line to my webpage.  I need to be able to do a search and replace on each line as I print so that it prints what I want.  Such as

$nabl_stand_file="standr.html";
open(nabl_standr, $nabl_stand_file) || die("Could not open file!");
@array_nabl=<nabl_standr>;
close(nabl_standr);

$search="href=";
$replace="href=http://www.nablbaseball.com/nabl/league/nabl_data.cgi?";

for ($x=18; $x<25; $x++)
  {
s/$search/$replace/ig;
print (substr($array_nabl[$x], 0, index($array_nabl[$x], "<td class=s3")));    
print "</tr>";
  }


The above does not seem to work for me.  I think you should be able to see what I am trying to do.  It works fine if I don't do the search replace.  Could someone please show me hoe to do this correctly.  Thanks.
Avatar of spoma
spoma

ASKER

I figured it out using this line

$array_nabl[$x] =~ s/$search/$replace/ig;
Actually the default variable $_ could be used, for instance:


for ( 18 .. 24 )
{
  $array_nabl[$_] =~ s/$search/$replace/ig;
  print (substr($array_nabl[$_], 0, index($array_nabl[$_], "<td class=s3")));    
  print "</tr>";
}
Avatar of spoma

ASKER

Mercantilum

I appreciate the feedback.  But how does that benefit over the way that I did using the variable $x
ASKER CERTIFIED SOLUTION
Avatar of Mercantilum
Mercantilum
Flag of Japan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial