Hi Ramprasad,

thanks for your answer, but see below for my comments.

On Wednesday 13 Apr 2011 14:30:36 Ramprasad Prasad wrote:
> On 13 April 2011 11:40, Shlomit Afgin <shlomit.af...@weizmann.ac.il> wrote:
> > Hi
> > 
> > 
> > I need to write regular expression that will capitalize the first letter
> > of each word in the string.
> > Word should be string with length that is greater or equal to 3 letters
> > 
> >  exclude the words 'and' and 'the'.
> > 
> > I tried:
> > $string = lc($string);
> > $string =~ s/\b(\w{3,}[(^the|^and)])\b/ucfirst($1)/ge;
> > but it not working so well.
> > 
> > You are matching two words every time here , I dont think you can do it
> 
> this way
> 

1. Always use "use strict;" and "use warnings;".

> This works
> while(<DATA>){

2. Input the line into an explicitly scoped variable:

        while (my $line = <DATA>) {

3. Add a space before the "{".

>     chomp;

>     $string = lc($_);
>     $string =~ s/\b(\w{3,})/subword($1)/ge;

Declare the $string variable using my.

>     print "$string\n";
> }
> sub subword {
>     return $_[0] if($_[0] =~/^(the|and)$/);
>     return ucfirst($_[0]);
> }

1. Don't hardcode positional variables inside arrays $_[0]:

http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments

2. You're using $_[0] more than once here, so it would better be a named 
varialbe.

3. The regex match should be written as:

        if ($word =~ m{\A(the|and)\z})

4. You're agai nmissing some surrounding spaces.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
What does "Zionism" mean? - http://shlom.in/def-zionism

Stray XSLT code causes more deaths than road accidents.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to