in reply to Re: Re: Updated code...
in thread Making a title/headline
(wrt to use strict ) Um, okay, but then this is bad:
foreach $word ( @wordlist ) { .... }
From this, it clearly looks like $word should be a lexical variable only visible in the foreach block.
And yeah, I didn't realize there was a print statement in the foreach loop ( I was printing out something completely irrelevant ). I see the problem: you're using @wordlist for both the foreach loop and the grep. Of course grep will return an non-empty array, silly ;-)
Update
use strict; my @nocapslist = qw/ in and the for it but to with about or nor because as that /; sub make_a_title { my $string = shift; $string =~ s/^\s+//; my @wordlist = split (/\s+/, ucfirst( $string ) ); join( ' ', map{ my $word = $_; ## not quite efficient like Ovid's;-) (grep{ $_ eq $word } @nocapslist) ? $word : ucfirst( $word + ); } @wordlist ); } my $str = "this is a sentence, for You and For me Blah Blah blah"; print make_a_title( $str );
|
|---|