in reply to Making a title/headline

if( !grep{ $_ eq $word } @wordlist ) { .... }

grep{ $word } would return all elements, by the way...

Replies are listed 'Best First'.
Updated code...
by Kickstart (Pilgrim) on Dec 15, 2001 at 04:09 UTC
    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

      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

      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

Re: Re: Making a title/headline
by strat (Canon) on Dec 17, 2001 at 18:45 UTC
    I prefer writing something like:
    unless(grep{ $_ eq $word } @wordlist ) { .... }
    I think it is more readable than a short !...

    Best regards,
    perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"