On Jan 15, John W. Krahn said:
>Tim Sargent wrote:
>>
>> 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,
>
>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;
> }
(?:\A|\b) can just be \b, unless one of the strings starts with a non-word
character.
But this is doing multiple passes over the string. One pass over the
string should be sufficient:
my $re =
join '|',
map { quotemeta($_) }
sort { length($b) <=> length($a) }
keys %lookup;
$line =~ s{\b($re)\b}{<span class="$lookup{$1}">$1</span>}g;
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]