[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Question - Substitution



On Wed, 23 Dec 1998 14:47:12 -0600, Mark Manning/Muniz Eng. wrote:

>              $theInfo[$k] =~ s/\s$theName\s/ <a
>href="c$j.html">$theName<\/a> /g;    
>                $theInfo[$k] =~ s/^$theName\s/<a
>href="c$j.html">$theName<\/a> /g;    
>                $theInfo[$k] =~ s/\s$theName$/ <a
>href="c$j.html">$theName<\/a>/g;    

Uh... repeated substitutions on the same string. Bad. Very bad. Although
that in this particular case it won't hurt that much.

>when I try to change this to
>
>                @theInfo =~ s/\s$theName\s/ <a
>href="c$j.html">$theName<\/a> /g;    
>                @theInfo =~ s/^$theName\s/<a
>href="c$j.html">$theName<\/a> /g;    
>                @theInfo =~ s/\s$theName$/ <a
>href="c$j.html">$theName<\/a>/g;    
>
>I get an error message saying MacPerl:
>
>Can't modify array deref in substitution at md line 1370, near
>"s/\s$theName\s/ <a href="c$j.html">$theName<\/a> /g;"
>md had compilation errors.

Yes. You can't do just the substitution on an array. Although, I must
say, it would make a sensible "correction" on the Perl syntax.

Anyway, I digress: you must, currently, apply the substitution(s) on
each array element.

Here's what I would make of it:

	my $j = 0;	
	foreach(@line) {
	      s/(^|\s)($theName)(\s|$)/$1<a href="c$j.html">$2<\/a>$3/g;

	      $j++;
	}

under the assumption that $j and $k were supposed to be the same thing.

	Bart.

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch