ucfirst() with "_" separator

Discussion in 'Perl Misc' started by gors, Feb 22, 2007.

  1. gors

    gors Guest

    We have big file has lines as:

    ITEM1 PRODUCT PRICE 3.47
    ITEM2 PRODUCT_DETAILS PRICE 4.47
    ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47

    Trouble is make 2nd word each line capital letter first, then lower case for
    every "_". Some 2nd word have "_" one or more time, other not. So we like
    make line each lokk like:

    ITEM1 Product PRICE 3.47
    ITEM2 Product_Details PRICE 4.47
    ITEM3 Product_Details_Again PRICE 5.47

    How operate on second word after each "_"? We try:

    echo "ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \
    perl -aF_ -ne '$stuff=ucfirst(lc $2); s/$2/$stuff/; print $_'

    but it not work make any change. Even:

    echo "PRODUCT_DETAILS_AGAIN" | \
    perl -aF_ -ne 'print ucfirst(lc $_);'

    produce only "Product_details_again" instead "Product_Details_Again".

    Someone can help please?
     
    gors, Feb 22, 2007
    #1
    1. Advertising

  2. gors

    Uri Guttman Guest

    >>>>> "g" == gors <1> writes:

    g> ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47

    g> Trouble is make 2nd word each line capital letter first, then lower
    g> case for every "_". Some 2nd word have "_" one or more time, other
    g> not. So we like make line each lokk like:

    g> ITEM3 Product_Details_Again PRICE 5.47

    g> How operate on second word after each "_"? We try:

    g> echo "ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \
    g> perl -aF_ -ne '$stuff=ucfirst(lc $2); s/$2/$stuff/; print $_'

    you aren't doing it for all the occurances of _. and that is not a good
    way to do it anyhow.

    read perldoc -q capital to see how it is done with words separated by
    blanks. you should be able to modify that to use _. if you need more
    help then show the new code here.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 22, 2007
    #2
    1. Advertising

  3. gors wrote:
    > We have big file has lines as:
    >
    > ITEM1 PRODUCT PRICE 3.47
    > ITEM2 PRODUCT_DETAILS PRICE 4.47
    > ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47
    >
    > Trouble is make 2nd word each line capital letter first, then lower case for
    > every "_". Some 2nd word have "_" one or more time, other not. So we like
    > make line each lokk like:
    >
    > ITEM1 Product PRICE 3.47
    > ITEM2 Product_Details PRICE 4.47
    > ITEM3 Product_Details_Again PRICE 5.47
    >
    > How operate on second word after each "_"? We try:
    >
    > echo "ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \
    > perl -aF_ -ne '$stuff=ucfirst(lc $2); s/$2/$stuff/; print $_'
    >
    > but it not work make any change. Even:
    >
    > echo "PRODUCT_DETAILS_AGAIN" | \
    > perl -aF_ -ne 'print ucfirst(lc $_);'
    >
    > produce only "Product_details_again" instead "Product_Details_Again".
    >
    > Someone can help please?


    $ echo "ITEM1 PRODUCT PRICE 3.47
    ITEM2 PRODUCT_DETAILS PRICE 4.47
    ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \

    perl -pe's/^(\S+\s+)(\S+)/$a = $1; ($b = $2) =~ s!([[:alpha:]]+)!\L\u$1!g;
    "$a$b"/e'
    ITEM1 Product PRICE 3.47
    ITEM2 Product_Details PRICE 4.47
    ITEM3 Product_Details_Again PRICE 5.47



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Feb 22, 2007
    #3
  4. gors

    -berlin.de Guest

    gors <1> wrote in comp.lang.perl.misc:
    > We have big file has lines as:
    >
    > ITEM1 PRODUCT PRICE 3.47
    > ITEM2 PRODUCT_DETAILS PRICE 4.47
    > ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47
    >
    > Trouble is make 2nd word each line capital letter first, then lower case for
    > every "_". Some 2nd word have "_" one or more time, other not. So we like
    > make line each lokk like:
    >
    > ITEM1 Product PRICE 3.47
    > ITEM2 Product_Details PRICE 4.47
    > ITEM3 Product_Details_Again PRICE 5.47
    >
    > How operate on second word after each "_"? We try:
    >
    > echo "ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \
    > perl -aF_ -ne '$stuff=ucfirst(lc $2); s/$2/$stuff/; print $_'
    >
    > but it not work make any change. Even:
    >
    > echo "PRODUCT_DETAILS_AGAIN" | \
    > perl -aF_ -ne 'print ucfirst(lc $_);'
    >
    > produce only "Product_details_again" instead "Product_Details_Again".


    while ( <DATA> ) {
    my @words = split;
    $_ = join '_', map ucfirst, split /_/, lc for $words[ 1];
    print "@words\n";
    }

    __DATA__
    ITEM1 PRODUCT PRICE 3.47
    ITEM2 PRODUCT_DETAILS PRICE 4.47
    ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47

    Anno
     
    -berlin.de, Feb 22, 2007
    #4
  5. gors

    Uri Guttman Guest

    >>>>> "MD" == Michele Dondi <> writes:

    MD> On Thu, 22 Feb 2007 12:14:51 +0100, Michele Dondi
    MD> <> wrote:

    >> There may be more compact solutions, but a fairly understandable one
    >> is
    >>
    >> s|(?<=\s)(\w+)(?=\s)|join '_', map ucfirst(lc), split /_/|e;


    MD> Well, of course there's a missing bit:

    MD> s|(?<=\s)(\w+)(?=\s)|join '_', map ucfirst(lc), split /_/, $1|e;

    MD> What's worst, (I tested the former and) it took me a while to
    MD> understand what was wrong... :-(

    MD> of course a slight modification a' la Anno's suggestion in this same
    MD> thread is better:

    MD> 's|(?<=\s)(\w+)(?=\s)|join '_', map ucfirst, split /_/, lc $1|e;

    i like this approach. it seems the term always starts with PRODUCT so i
    assumed that. i sent the 3 lines from the OP into this one liner and it
    works. not often you get to see nested s/// ops. :)


    perl -lpe 's/(PROD\w+)/$x = $1 ; $x =~ s{([A-Z]+)}{\u\L$1}g ; $x/e'
    ITEM1 PRODUCT PRICE 3.47
    ITEM1 Product PRICE 3.47
    ITEM2 PRODUCT_DETAILS PRICE 4.47
    ITEM2 Product_Details PRICE 4.47
    ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47
    ITEM3 Product_Details_Again PRICE 5.47

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 22, 2007
    #5
  6. gors

    Dr.Ruud Guest

    Michele Dondi schreef:

    > s|(?<=\s)(\w+)(?=\s)|join '_', map ucfirst, split /_/, lc $1|e;



    Shorter:

    s|(?<=\s)(\w+)|join '_', map ucfirst, split /_/, lc $1|e;

    s|(?<= )(\w+)|join _=>map ucfirst,split _=>lc$1|e;

    Alternative approach:

    local $";
    s|(?<=\s)(\w+)|@{[map ucfirst, split /(_)/, lc $1]}|;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Feb 23, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Javier
    Replies:
    5
    Views:
    1,034
    Gunnar Hjalmarsson
    Jan 16, 2004
  2. Kevin Spencer

    Re: Separator in panel

    Kevin Spencer, Aug 20, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    426
    Kevin Spencer
    Aug 20, 2003
  3. DC Gringo
    Replies:
    3
    Views:
    6,173
    mikeb
    Aug 19, 2004
  4. Kaja
    Replies:
    0
    Views:
    611
  5. hansiman
    Replies:
    1
    Views:
    19,494
    =?Utf-8?B?S2VuIENveCBbTWljcm9zb2Z0IE1WUF0=?=
    Oct 8, 2004
Loading...

Share This Page