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
Solved

Search and Replace of text with array

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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