Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Find First character of each word in a string

by sweetblood (Parson)
on Nov 24, 2010 at 00:09 UTC ( #873319=note: print w/ replies, xml ) Need Help??


in reply to Find First character of each word in a string

Why not try it yourself and then post your results and your code. Then mention problems that you encounter and errors.

Sweetblood


Comment on Re: Find First character of each word in a string
Re^2: Find First character of each word in a string
by kevyt (Scribe) on Nov 24, 2010 at 00:22 UTC
    This works but I dont like it.
    $str = 'my cardio workout'; @s = split (/\s/,$str); foreach (@s){ my $new_str = substr ($_,0,1); $new_str = ucfirst ($new_str); $ab .= $new_str; } print "AB is $ab\n";

      If that's the way things are, then maybe:

      >perl -wMstrict -le "my $str = 'eternal corruption defilement'; ;; $str =~ s{ \b ([[:alpha:]]) [[:lower:]]* \s* }{\U$1}xmsg; ;; print qq{'$str'}; " 'ECD'

      Update: Removed an unnecessary level of capture grouping in regex above. Also:  [[:lower:]]* might be better as  [[:alpha:]]* instead.

      Update: Or even:

      >perl -wMstrict -le "my $str = 'eternal corruption defilement'; ;; $str =~ s{ (\b [[:alpha:]]+ \s*) }{ uc substr $1, 0, 1 }xmsge; ;; print qq{'$str'}; " 'ECD'
        Thanks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-12-29 10:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (186 votes), past polls