tr problem

Discussion in 'Perl Misc' started by IanW, Sep 30, 2004.

  1. IanW

    IanW Guest

    If I have a string like this

    "listname A short description of the list"

    and I want to make the "listname" part of the string upper case, how can I
    do this with tr///?

    Incidentally, "listname" will never have any spaces in it and will always be
    at the beginning of the string, so I had thought that sth like this might
    work, but it just makes the whole thing upper case:

    $_ =~ tr/^a-z+(?=\s+)/A-Z/;

    Ian
    IanW, Sep 30, 2004
    #1
    1. Advertising

  2. IanW

    Tore Aursand Guest

    On Thu, 30 Sep 2004 09:13:07 +0100, IanW wrote:
    > If I have a string like this
    >
    > "listname A short description of the list"
    >
    > and I want to make the "listname" part of the string upper case, how can I
    > do this with tr///?
    >
    > Incidentally, "listname" will never have any spaces in it and will always be
    > at the beginning of the string, so I had thought that sth like this might
    > work, but it just makes the whole thing upper case:
    >
    > $_ =~ tr/^a-z+(?=\s+)/A-Z/;


    What you really is saying, is that the _first word_ in the sentence should
    be converted to uppercase?

    s{^(.+?\s+)}{ uc($1) }e;


    --
    Tore Aursand <>
    "Why shouldn't truth be stranger than fiction? Fiction, after all, has
    to make sense." (Mark Twain)
    Tore Aursand, Sep 30, 2004
    #2
    1. Advertising

  3. IanW

    Anno Siegel Guest

    IanW <> wrote in comp.lang.perl.misc:
    > If I have a string like this
    >
    > "listname A short description of the list"
    >
    > and I want to make the "listname" part of the string upper case, how can I
    > do this with tr///?


    $_ = "listname A short description of the list";
    substr( $_, 0, index $_, ' ') =~ tr/a-z/A-Z/;

    But see below.

    > Incidentally, "listname" will never have any spaces in it and will always be
    > at the beginning of the string, so I had thought that sth like this might
    > work, but it just makes the whole thing upper case:
    >
    > $_ =~ tr/^a-z+(?=\s+)/A-Z/;


    tr/// works on plain strings (whose characters determine what is
    changed), not on regular expressions. Also, it isn't the best tool
    to change case -- Perl's built-in functions lc and uc do a better job
    of it, as they respect locale. Use a regular expression to do that.

    Anno
    Anno Siegel, Sep 30, 2004
    #3
  4. IanW wrote:
    > If I have a string like this
    >
    > "listname A short description of the list"
    >
    > and I want to make the "listname" part of the string upper case, how can I
    > do this with tr///?
    >
    > Incidentally, "listname" will never have any spaces in it and will always be
    > at the beginning of the string, so I had thought that sth like this might
    > work, but it just makes the whole thing upper case:
    >
    > $_ =~ tr/^a-z+(?=\s+)/A-Z/;


    tr/// translates the characters on the left with the corresponding characters
    on the right so '^' will be replaced with 'A', 'a' with 'B', 'b' with 'C' and
    so on up to the characters 'z', '+', '(', '?', '=', 's', '+' and ')' which
    will all be replaced with 'Z'. You need to use the substitution operator instead.

    s/^(a-z+)(?=\s)/\U$1/;


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 30, 2004
    #4
  5. IanW

    IanW Guest

    "Anno Siegel" <-berlin.de> wrote in message
    news:cjgiiq$7c4$-Berlin.DE...
    [snip]
    > tr/// works on plain strings (whose characters determine what is
    > changed), not on regular expressions. Also, it isn't the best tool
    > to change case -- Perl's built-in functions lc and uc do a better job
    > of it, as they respect locale. Use a regular expression to do that.


    Yes, I see what you mean, from Tore's reply... I've just looked up the "e"
    modifier on regexps - I didn't realise you could include functions like uc
    in a regexp.. cool :)

    Thanks guys
    IanW, Sep 30, 2004
    #5
  6. IanW

    IanW Guest

    "John W. Krahn" <> wrote in message
    news:1fQ6d.2699$eq.1836@edtnps84...
    > IanW wrote:
    > > If I have a string like this
    > >
    > > "listname A short description of the list"
    > >
    > > and I want to make the "listname" part of the string upper case, how can

    I
    > > do this with tr///?
    > >
    > > Incidentally, "listname" will never have any spaces in it and will

    always be
    > > at the beginning of the string, so I had thought that sth like this

    might
    > > work, but it just makes the whole thing upper case:
    > >
    > > $_ =~ tr/^a-z+(?=\s+)/A-Z/;

    >
    > tr/// translates the characters on the left with the corresponding

    characters
    > on the right so '^' will be replaced with 'A', 'a' with 'B', 'b' with 'C'

    and
    > so on up to the characters 'z', '+', '(', '?', '=', 's', '+' and ')' which
    > will all be replaced with 'Z'. You need to use the substitution operator

    instead.
    >
    > s/^(a-z+)(?=\s)/\U$1/;


    \U is another new one one me! I need a new reference book ;-)

    Btw, I could shorten that regexp more by doing:

    $_ =~ s/^(\S+)/\U$1/;

    Thanks
    IanW, Sep 30, 2004
    #6
  7. IanW

    Joe Smith Guest

    IanW wrote:

    > If I have a string like this
    >
    > "listname A short description of the list"
    >
    > and I want to make the "listname" part of the string upper case, how can I
    > do this with tr///?


    You can't. If you ask tr/// to change one 'l' to 'L', it will do so for all.

    > Incidentally, "listname" will never have any spaces in it and will always be
    > at the beginning of the string,


    'beginning of the string' == 'time to use a regex, such as s///, not tr///.'

    s/^(\w*)/\U$1/; # If words do not include punctuation
    s/^(\S*)/\U$1/; # If they do

    -Joe
    Joe Smith, Sep 30, 2004
    #7
  8. IanW <> wrote:

    > \U is another new one one me! I need a new reference book ;-)



    No new book is required.

    Just read the "Quote and Quote-like Operators" section in
    the documentation for the programming language that you are using:

    perldoc perlop



    > Btw, I could shorten that regexp more by doing:
    >
    > $_ =~ s/^(\S+)/\U$1/;



    If short is your goal, then you can improve it even further:

    s/^(\S+)/\U$1/;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 30, 2004
    #8
  9. IanW wrote:
    > If I have a string like this
    >
    > "listname A short description of the list"
    >
    > and I want to make the "listname" part of the string upper case, how
    > can I do this with tr///?


    Why do you want to use tr?
    Most people would probably choose uc() instead, in particular because uc()
    will work worldwide while coding the correct character lists for non-English
    charactes for tr must be a major endevour.

    > Incidentally, "listname" will never have any spaces in it and will
    > always be at the beginning of the string, so I had thought that sth
    > like this might work, but it just makes the whole thing upper case:
    >
    > $_ =~ tr/^a-z+(?=\s+)/A-Z/;


    Where did you get the idea that tr would use REs?

    jue
    Jürgen Exner, Sep 30, 2004
    #9
  10. IanW

    Ala Qumsieh Guest

    Christian Winter wrote:
    > Tad McClellan schrieb:
    >
    >> IanW <> wrote:

    >
    > [...]
    >
    >>
    >>> Btw, I could shorten that regexp more by doing:
    >>>
    >>> $_ =~ s/^(\S+)/\U$1/;

    >>
    >>
    >>
    >>
    >> If short is your goal, then you can improve it even further:
    >>
    >> s/^(\S+)/\U$1/;

    >
    >
    > Or even further:
    >
    > s/^\S+/\U$&/;


    Further still:

    s/\S+/\U$&/;

    --Ala
    Ala Qumsieh, Sep 30, 2004
    #10
  11. IanW

    Ben Morrow Guest

    Quoth Ala Qumsieh <>:
    > Christian Winter wrote:
    > > Tad McClellan schrieb:
    > >> IanW <> wrote:
    > >>
    > >>> Btw, I could shorten that regexp more by doing:
    > >>>
    > >>> $_ =~ s/^(\S+)/\U$1/;
    > >>
    > >> If short is your goal, then you can improve it even further:
    > >>
    > >> s/^(\S+)/\U$1/;

    > >
    > > s/^\S+/\U$&/;

    >
    > s/\S+/\U$&/;


    Not equivalent. Consider $_ = ' a'.

    Ben

    --
    If I were a butterfly I'd live for a day, / I would be free, just blowing away.
    This cruel country has driven me down / Teased me and lied, teased me and lied.
    I've only sad stories to tell to this town: / My dreams have withered and died.
    (Kate Rusby)
    Ben Morrow, Sep 30, 2004
    #11
  12. IanW

    Eric Amick Guest

    On Thu, 30 Sep 2004 15:33:20 +0200, Christian Winter
    <> wrote:

    >Tad McClellan schrieb:
    >> IanW <> wrote:

    >[...]
    >>
    >>>Btw, I could shorten that regexp more by doing:
    >>>
    >>>$_ =~ s/^(\S+)/\U$1/;

    >>
    >>
    >>
    >> If short is your goal, then you can improve it even further:
    >>
    >> s/^(\S+)/\U$1/;

    >
    >Or even further:
    >
    >s/^\S+/\U$&/;


    At the cost of making all regexes run slower. That's not much of a gain,
    especially in this case.

    --
    Eric Amick
    Columbia, MD
    Eric Amick, Oct 1, 2004
    #12
    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. Andreas Suurkuusk
    Replies:
    0
    Views:
    3,950
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,136
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    1,943
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    379
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    530
    Bullschmidt
    May 11, 2004
Loading...

Share This Page