Solved

Search and Replace of text with array

Posted on 2004-04-22
6
187 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell code to list items in dropdown 6 113
storing csv file in table variable in Python 2 94
Permission on folders 6 35
Powershell PGP and SFTP 1 47
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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…

734 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