On 4/18/05, N. Ganesh Babu <[EMAIL PROTECTED]> wrote:
> Dear All
> 
> I want to do capitalisation of this text. I am using the following code.
> It works fine with all other lines.
> 
> $line="Level A (Grade 1 reading level)";
> 
>     @words=split(/ /,$line);
>     for($i=0;$i<=$#words;$i++)
>     {
>     $words[$i]=~s!($words[$i])!\u\L$1!gi;
>     print "$words[$i]\n";
>     }
> 
> This code is showing the following error: The main reason I understood
> is the presence of (  and ) in the text.
> 
> perl test.pl
> Level
> A
> Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE (Grade)/ at
> test.pl line 6.
> 
> Compilation exited abnormally with code 255 at Mon Apr 18 15:22:33
> 
> Please help me in this regard to solve this problem.
> 
> Regards,
> Ganesh

Ganesh,

The problem you're having here is because your double quoted string is
being interpolated.

$words[2] = "(Grade"

Therefore you regex turns into"

s/((Grade)/\u\L($1)/gi

But you don't need to interpolate the variable, $words[$i] =~
m/$words[$i]/ is redundant; it's given. The same goes for /g: yuo know
you're matching the ientire sting, the match can't possibly repeat.

Also, ($1) will add extra parentheses in the output.

What you're looking for is something like this:

foreach my $word (split / /, $line) {
   $word =~ s/(.*)/\u\L$1/;
   print "$word ";
}

or better yet, skip the loop all together:

$line =~ /(\S+)/\u\L$1/g ;

This assumes, of course, that all you data is like your sample, and
the letter following the opening parenthesis is always capitalized. 
Otherwise, it gets more complicated.

HTH,

--jay

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to