?
Solved

Search and Replace of text with array

Posted on 2004-04-22
6
Medium Priority
?
189 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 400 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

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 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