Solved

Search and Replace of text with array

Posted on 2004-04-22
6
188 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:spoma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 

Author Comment

by:spoma
ID: 10889847
I figured it out using this line

$array_nabl[$x] =~ s/$search/$replace/ig;
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10895554
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>";
}
0
 

Author Comment

by:spoma
ID: 10896109
Mercantilum

I appreciate the feedback.  But how does that benefit over the way that I did using the variable $x
0
 
LVL 10

Accepted Solution

by:
Mercantilum earned 100 total points
ID: 10896275
I guess there is a slight benefit in terms of optimization (while Perl is doing that very well), since there is less variables, and no test or incrementation.
The major benefit to my mind is the readability (when one get used to perl). It is a good way to stick more to perl than to C :)

The loop could have even been

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

Note:
- yes, altering $_ will actually alter the array item.
- no more indexing of array
- easier to read
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question