Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Making a title/headline

by Kickstart (Pilgrim)
on Dec 15, 2001 at 03:44 UTC ( [id://132131]=perlquestion: print w/replies, xml ) Need Help??

Kickstart has asked for the wisdom of the Perl Monks concerning the following question:

This subroutine is intended to convert a string into a format suitable for a headline or title, but seems to really dislike the "if !(grep {$word} @wordlist)" line.

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 my $word (@wordlist) { if !(grep {$word} @wordlist) { ucfirst $word; } return join(' ', @wordlist); }

Kickstart

Replies are listed 'Best First'.
(Ovid) Re: Making a title/headline
by Ovid (Cardinal) on Dec 15, 2001 at 04:27 UTC

    You were on the right track.

    use strict; my $title = "this and that are the way to go"; $title = make_a_title( $title ); print $title; sub make_a_title { my $title = shift; my @nocapslist = qw( in and the for it but to with about or nor because as that ); my %nocapslist; @nocapslist{ @nocapslist } = undef; my @wordlist = split /\s+/, $title; $wordlist[0] = ucfirst $wordlist[0]; foreach (@wordlist) { $_ = ucfirst if ! exists $nocapslist{ $_ }; } return join ' ', @wordlist; }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Very helpful! I'm curious though...what is done with these two lines?

      my %nocapslist; @nocapslist{ @nocapslist } = undef;

      Kickstart

        I declared a hash and then used a hash slice to set all of the keys in the has equal to the values of the array and all of the hash values equal to 'undef'. If you'd like to see it better, you can add these two lines after this code:

        use Data::Dumper; print Dumper \%nocapslist;

        That produces:

        $VAR1 = { 'nor' => undef, 'with' => undef, 'in' => undef, 'or' => undef, 'about' => undef, 'that' => undef, 'as' => undef, 'the' => undef, 'and' => undef, 'for' => undef, 'it' => undef, 'but' => undef, 'to' => undef, 'because' => undef };

        Also, note that we're really not "setting" the values to undef. That's just setting the first hash value to undef and the rest default to that. If you wanted to set all of the values to 1, for example, you'd do something like this:

        @nocapslist{ @nocapslist } = (1) x @nocapslist;

        There are two benefits to using a hash instead of a grep. The first is that the hash lookup is faster. I don't really consider that a benefit, though, because it's always better to optimize for clarity than speed. I changed it to a hash for the second reason: using exists is much easier than using a grep, in terms of programmers understanding it.

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        This uses a hash as hashslice, and so sets the values of the keys contained by @nocapslist to undef. I'd prefer
        @nocapslist{ @nocapslist } = ();
        because in my eyes, undef is a scalar value...

        But it's just a matter of different tastes ;-)

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

Re: Making a title/headline
by dws (Chancellor) on Dec 15, 2001 at 03:49 UTC
    Change   if !(grep {$word} @wordlist) { to   if (!grep {$word} @wordlist) {
      Ok...still doesn't work (also fixed a missing bracket). I want to send this a variable containing a string, and have it convert it to a headline. Kickstart
Re: Making a title/headline
by lestrrat (Deacon) on Dec 15, 2001 at 03:53 UTC
    if( !grep{ $_ eq $word } @wordlist ) { .... }

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

      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

        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

      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"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found