Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Find First character of each word in a string

by kevyt (Scribe)
on Nov 24, 2010 at 00:22 UTC ( [id://873321]=note: print w/replies, xml ) Need Help??


in reply to Re: Find First character of each word in a string
in thread Find First character of each word in a string

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";

Replies are listed 'Best First'.
Re^3: Find First character of each word in a string
by AnomalousMonk (Archbishop) on Nov 24, 2010 at 00:45 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://873321]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2026-01-19 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (122 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.