Hi, Brian, :)

I'm by no means a Perl expert, but I was intrigued that you asked a
question I *might* actually be able to answer, so here goes:

On Tue, 11 Sep 2001, Bradshaw, Brian wrote:

> I have a string that I want to substitute words for each <select>
> tag. I am pretty sure I want to split the string at the <select>
> tags, then join pieces with each answer in its proper place.

Seems reasonable.  I took your sample string and created this script,
which seemed to work:

---------- cut ----------

#!/usr/bin/perl

# Store the text you provided in your e-mail as a big string.
$text = <<BOB;
If you compare plant and animal cells, you will discover differences. Plant
cells are stiff and rigid. They have a(n) <select name="a1"><option
value="cell membrane">cell membrane</option><option
value="cytoplasm">cytoplasm</option><option value="cell wall">cell
wall</option><option value="cell nuclei">cell nuclei</option><option
value="BLANK" selected>Select your answer</option></select> that protects
the plant cell. Animal cells have only a thin outer covering called a(n)
<select name="a2"><option value="power plant">power plant</option><option
value="cell wall">cell wall</option><option value="cell membrane">cell
membrane</option><option value="chloroplasts">chloroplasts</option><option
value="BLANK" selected>Select your answer</option></select> to protect them.
Plant cells only have one or two holding bins called <select
name="a3"><option value="cell wall">cell wall</option><option
value="chloroplasts">chloroplasts</option><option
value="vacuoles">vacuoles</option><option
value="cytoplasm">cytoplasm</option><option value="BLANK" selected>Select
your answer</option></select>. Animal cells have many vacuoles. Plant cells
have food factories called <select name="a4"><option
value="vacuoles">vacuoles</option><option
value="cytoplasm">cytoplasm</option><option value="cell nuclei">cell
nuclei</option><option value="chloroplasts">chloroplasts</option><option
value="BLANK" selected>Select your answer</option></select> which animal
cells don't have. Plant and animal cells also have other parts. Both have
<select name="a5"><option value="cell wall">cell wall</option><option
value="vacuoles">vacuoles</option><option value="cell nuclei">cell
nuclei</option><option value="cell membrane">cell membrane</option><option
value="BLANK" selected>Select your answer</option></select> which are found
in the center of the cells. They have a jellylike substance that fills the
cells called <select name="a6"><option
value="chloroplasts">chloroplasts</option><option
value="cytoplasm">cytoplasm</option><option value="cell membrane">cell
membrane</option><option value="vacuoles">vacuoles</option><option
value="BLANK" selected>Select your answer</option></select>. Both types of
cells also have a part that uses fuel from food for energy. It's like the
cell's <select name="a7"><option value="cell membrane">cell
membrane</option><option value="power plant">power plant</option><option
value="vacuoles">vacuoles</option><option
value="chloroplasts">chloroplasts</option><option value="BLANK"
selected>Select your answer</option></select>.
BOB

# Remove new lines, splitting into an array.
@text = split( "\n", $text );

# Now weld the string back together, with spaces instead of newlines!
$text = join( " ", @text );

# Find the string bounded by "<select " and "/select>".  The '.+?'
# finds all characters but isn't greedy; ".+" would match all of the
# characters between the first "<select " and the *last* "/select>".
# Store this match as $1.
while( $text =~ /(<select .+?\/select>)/ )
{  # Use the backreference $1, replacing the matched string with "!x!".
   $text =~ s/$1/!x!/;
}

# Print out the modified string.
print "$text\n";

---------- cut ----------

This seemed to work for me.  Here's the result of the run:

$ ./testit3.pl
If you compare plant and animal cells, you will discover differences. Plant cells are 
stiff and rigid. They have a(n) !x! that protects the plant cell. Animal cells have 
only a thin outer covering called a(n) !x! to protect them. Plant cells only have one 
or two holding bins called !x!. Animal cells have many vacuoles. Plant cells have food 
factories called !x! which animal cells don't have. Plant and animal cells also have 
other parts. Both have !x! which are found in the center of the cells. They have a 
jellylike substance that fills the cells called !x!. Both types of cells also have a 
part that uses fuel from food for energy. It's like the cell's !x!.


Yes, that is *one* long line.  If you want, you could do the "join"
using some other character than space, such as '@', so that you could
then replace all of the '@'s that were left with newlines and restore
some semblance of the original pargraph's spacing.

I hope this was helpful for you - it was a good learning experience
for me. :)

---Jason Tiller


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to