Solved

Search and Replace of text with array

Posted on 2004-04-22
6
174 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
  • 2
  • 2
6 Comments
 

Author Comment

by:spoma
Comment Utility
I figured it out using this line

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

Expert Comment

by:Mercantilum
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
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…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now