Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Updated code...

by Kickstart (Pilgrim)
on Dec 15, 2001 at 04:09 UTC ( [id://132145]=note: print w/replies, xml ) Need Help??


in reply to Re: Making a title/headline
in thread Making a title/headline

sub make_a_title { my @nocapslist = ( 'in', 'and', 'the', 'for', 'it', 'but', 'to', 'with', 'about', 'or', 'nor', 'because', 'as', 'that' ); my @wordlist = split (/\s+/, $_[0]); ucfirst ($wordlist[0]); foreach $word (@wordlist) { if (!grep {$_ eq $word} @wordlist) { ucfirst ($word); print $word; } return join(' ', @wordlist); } }

Still just returns blank. With -w and 'use strict' it bitches about using ucfirst in void context. What am I missing here?

Kickstart

Replies are listed 'Best First'.
Re: Updated code...
by lestrrat (Deacon) on Dec 15, 2001 at 04:23 UTC

    perldoc -f ucfirst clearly states:

    ucfirst EXPR ucfirst Returns the value of EXPR with the first character in uppercase. This + is the internal function implementing the \u escape in double-quoted stri +ngs. Respects current LC_CTYPE locale if use locale in force. See perlloca +le. If EXPR is omitted, uses $_.

    So ucfirst($foo) returns a string with the first characterer in upper case, but doesn't change the the value in the scalar. Hence, if you're not assining the result to some value nothing is gained from it, and the warning "Useless use of upper case first in void context" is generated.

    anyway, I don't know why your return value is not correct, because after I fixed those errors, it printec out something ( I didn't bother checking if it was correct, though ).

    Since you don't seem to be using strict, I suspect that the cause of your problem is somewhere else other than in this sub

      I am using 'use strict' and 'perl -w'. Updated code below. still doesn't quite get it.

      Kickstart

        (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 );
Giving up on ucfirst.
by Kickstart (Pilgrim) on Dec 15, 2001 at 04:24 UTC
    Here's is more updated code, definitely better, in that it actually works on the first word, now I need it to work in the loop.

    sub make_a_title { my @nocapslist = ( 'in', 'and', 'the', 'for', 'it', 'but', 'to', 'with', 'about', 'or', 'nor', 'because', 'as', 'that' ); my @wordlist = split (/\s+/, $_[0]); $wordlist[0] =~ s/(\w+)/\u\L$1/g; print "@wordlist\n"; foreach $word (@wordlist) { unless (grep {$_ eq $word} @wordlist) { $word =~ s/(\w+)/\u\L$1/g; print $word; } return join(' ', @wordlist); } }

    Kickstart

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://132145]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2024-04-25 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found