Tim Sargent wrote: > > Hello - Hello,
> I'm attempting to develop a simple script that will read a text file and > turn it into an HTML page for me. It's not CGI - I run the script from the > command line and then upload the resultant file to the web server. > > In a nutshell, within this text file certain words have a specific color > or style assigned to them. What I want to do is be able to read a line a > time, and make all the changes to the line (if any) without altering the > actual spacing of the lines. For example: > > Unaltered (straight text) line: > ------------------------------- > GS5 launches 3 plasma torpedoes (GS5.p1-3), warhead strength 20, speed > 32, > > Formatted line: > --------------- > <SPAN CLASS="green">GS5</SPAN> <SPAN CLASS="launch">launches</SPAN> 3 > plasma torpedoes (<SPAN CLASS="green-atu">GS5.p1-3</SPAN>), warhead strength > 20, speed 32, > > Currently I'm using a bunch of IF statements to try and find these > words, but it has difficulty with duplicate words in the same line (which is > a legal occurence for the page), and words that contain other words (like > GS5.p1-3 contains GS5). > > Code sample follows: > > if ($line =~ /\s+([BS]\d{1,2})\s+) { > $newText = "<SPAN CLASS="blue">$1</SPAN>"; > $line =~ s/$1/$newText/g; > } > > and so forth. I'd prefer an efficient and neat method to do all this, > but am rather stuck. Any pointers that can get me started down the right > path would be greatly appreciated...I know there's an answer out there, I'm > just having a devil of a time finding it. Sorry for the lengthy post. First of all your code sample could be written as: $line =~ s-\s+([BS]\d{1,2})\s+-<SPAN CLASS="blue">$1</SPAN>-g; In other words, there is no point in using an if statement to find a match and then doing a substitution when a substitution itself will do the job. Second, if all your search words are unique I would use a hash. my %lookup = ( 'GS5' => 'green', 'launches' => 'launch', 'GS5.p1-3' => 'green-atu', ); for my $word ( sort { length $b <=> length $a } keys %lookup ) { $line =~ s[(?:\A|\b)\Q$word\E(?:\b|\Z)] [<SPAN CLASS="$lookup{$word}">$word</SPAN>]g; } John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]