Newbie Q - Nicer way to lc something?

Discussion in 'Perl Misc' started by Chris Smith, Oct 1, 2003.

  1. Chris Smith

    Chris Smith Guest

    Hi,

    Is there a nicer way of doing the following?

    $a = lc($a);

    Doesn't look "perlish" if you know what I mean.

    Cheers,

    --
    Chris Smith
    http://www.infinitemonkeys.org.uk/
     
    Chris Smith, Oct 1, 2003
    #1
    1. Advertising

  2. Chris Smith

    Tore Aursand Guest

    On Wed, 01 Oct 2003 13:41:02 +0100, Chris Smith wrote:
    > Is there a nicer way of doing the following?
    >
    > $a = lc($a);
    >
    > Doesn't look "perlish" if you know what I mean.


    No. I don't know what you mean. What do you mean? Do you find solutions
    like this one more "perlish"?

    $a =~ tr/A-Z/a-z/;

    If so, you shouldn't be using Perl. :)


    --
    Tore Aursand <>
     
    Tore Aursand, Oct 1, 2003
    #2
    1. Advertising

  3. Chris Smith wrote:
    > Is there a nicer way of doing the following?
    >
    > $a = lc($a);
    >
    > Doesn't look "perlish" if you know what I mean.


    I don't know that. Looks nice enough to me.

    You'd better avoid using $a (as well as $b) outside the sort()
    function, though.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 1, 2003
    #3
  4. Chris Smith

    Chris Smith Guest

    Tore Aursand wrote:

    > On Wed, 01 Oct 2003 13:41:02 +0100, Chris Smith wrote:
    >> Is there a nicer way of doing the following?
    >>
    >> $a = lc($a);
    >>
    >> Doesn't look "perlish" if you know what I mean.

    >
    > No. I don't know what you mean. What do you mean? Do you find solutions
    > like this one more "perlish"?
    >
    > $a =~ tr/A-Z/a-z/;
    >
    > If so, you shouldn't be using Perl. :)


    I'm talking more like:

    lc $a;

    And return the value in $a rather than $_

    $a=lc($a);

    is very BASIC-like if you know what I mean.

    Perhaps it's just me being fussy :/

    --
    Chris Smith
    http://www.infinitemonkeys.org.uk/
     
    Chris Smith, Oct 1, 2003
    #4
  5. Chris Smith

    Chris Smith Guest

    Gunnar Hjalmarsson wrote:

    > Chris Smith wrote:
    >> Is there a nicer way of doing the following?
    >>
    >> $a = lc($a);
    >>
    >> Doesn't look "perlish" if you know what I mean.

    >
    > I don't know that. Looks nice enough to me.
    >
    > You'd better avoid using $a (as well as $b) outside the sort()
    > function, though.


    Have done - the real code is:

    $article = lc($article);

    Just shortening it for the example.

    Thanks for the tip though - didn't know $a and $b were "recommended
    avoidables".

    --
    Chris Smith
    http://www.infinitemonkeys.org.uk/
     
    Chris Smith, Oct 1, 2003
    #5
  6. Chris Smith wrote:
    > $a=lc($a);
    > is very BASIC-like if you know what I mean.


    Does :

    $a=lc$a;

    look better? :))

    RobTM:)
     
    Robert Szczygiel, Oct 1, 2003
    #6
  7. Chris Smith

    Chris Smith Guest

    Robert Szczygiel wrote:

    > Chris Smith wrote:
    >> $a=lc($a);
    >> is very BASIC-like if you know what I mean.

    >
    > Does :
    >
    > $a=lc$a;
    >
    > look better? :))


    Even worse. I get your point ;-)

    --
    Chris Smith
    http://www.infinitemonkeys.org.uk/
     
    Chris Smith, Oct 1, 2003
    #7
  8. Chris Smith <> wrote:

    > Is there a nicer way of doing the following?



    Maybe.


    > $a = lc($a);



    Show us where $a _first_ gets its value.

    You can lowercase it there without needing a separate step.

    eg:

    $a = lc $1;

    $a = "\Qother $1 stuff"; # OTHER $1 STUFF

    $a = "other \Q$1 stuff"; # other $1 STUFF

    $a = "other \Q$1\E stuff"; # other $1 stuff


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 1, 2003
    #8
  9. Chris Smith

    Uri Guttman Guest

    >>>>> "CS" == Chris Smith <> writes:

    CS> lc $a;

    CS> And return the value in $a rather than $_

    what return value in $_??


    in general the only things that implicitly set $_ are loop contructs
    (for, while( <FH> ), map, grep). i can't think of any function that will
    just set $_ (i could be wrong). many functions use $_ as a default arg
    which is not the same as a default return.

    CS> $a=lc($a);

    CS> is very BASIC-like if you know what I mean.

    CS> Perhaps it's just me being fussy :/

    yes.

    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
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
     
    Uri Guttman, Oct 1, 2003
    #9
  10. Chris Smith

    Tore Aursand Guest

    On Wed, 01 Oct 2003 14:51:05 +0100, Chris Smith wrote:
    >>> Is there a nicer way of doing the following?
    >>>
    >>> $a = lc($a);
    >>>
    >>> Doesn't look "perlish" if you know what I mean.


    >> No. I don't know what you mean. What do you mean? Do you find
    >> solutions like this one more "perlish"?
    >>
    >> $a =~ tr/A-Z/a-z/;
    >>
    >> If so, you shouldn't be using Perl. :)


    > I'm talking more like:
    >
    > lc $a;
    >
    > And return the value in $a rather than $_


    Sure, but what happens when you _want_ to check the return value of lc()?

    my $str = "Don't mess with this text";
    if ( lc( $str ) eq "don't mess with this text" ) {
    # I'm not messing
    }

    > Perhaps it's just me being fussy :/


    Yup.


    --
    Tore Aursand <>
     
    Tore Aursand, Oct 1, 2003
    #10
  11. Tad McClellan <> wrote:
    > Chris Smith <> wrote:
    >
    > > Is there a nicer way of doing the following?
    > > $a = lc($a);

    >
    > $a = "\Qother $1 stuff"; # OTHER $1 STUFF
    >
    > $a = "other \Q$1 stuff"; # other $1 STUFF
    >
    > $a = "other \Q$1\E stuff"; # other $1 stuff


    You meant "\U" instead of "\Q" ?

    --
    Steve
     
    Steve Grazzini, Oct 1, 2003
    #11
  12. Chris Smith

    Matija Papec Guest

    X-Ftn-To: Chris Smith

    Chris Smith <> wrote:
    >Is there a nicer way of doing the following?
    >
    > $a = lc($a);
    >
    >Doesn't look "perlish" if you know what I mean.


    Your example is perfectly fine unless you want to obfuscate deliberately,

    $a = "\L$a"; #also fine

    $_ = lc for $a; #don't do this



    --
    Matija
     
    Matija Papec, Oct 1, 2003
    #12
  13. Chris Smith

    Jay Tilton Guest

    Chris Smith <> wrote:

    : Tore Aursand wrote:
    :
    : > On Wed, 01 Oct 2003 13:41:02 +0100, Chris Smith wrote:
    : >> Is there a nicer way of doing the following?
    : >>
    : >> $a = lc($a);
    : >>
    : >> Doesn't look "perlish" if you know what I mean.
    : >
    : > No. I don't know what you mean. What do you mean? Do you find solutions
    : > like this one more "perlish"?
    : >
    : > $a =~ tr/A-Z/a-z/;
    : >
    : > If so, you shouldn't be using Perl. :)
    :
    : I'm talking more like:
    :
    : lc $a;
    :
    : And return the value in $a rather than $_

    Do you mean you would like the lc() function to alter its argument
    in-place? You can have that.

    use subs 'lc';
    sub lc {
    my $arg = \( @_ ? $_[0] : $_ );
    $$arg = CORE::lc $$arg;
    }

    $_ = 'HELLO, WORLD!';
    print;
    lc;
    print;

    Perl programmers tend to like functions that have minimal side-effects,
    but the proposed function is nothing but a side-effect. To "look
    perlish," a significant perlish quality is lost.

    : $a=lc($a);
    :
    : is very BASIC-like if you know what I mean.

    What language is it like if I do not know what you mean? :)

    Readers of technical newsgroups appreciate precision. You might want to
    avoid habitual use of imprecise language like "if you know what I mean."
     
    Jay Tilton, Oct 2, 2003
    #13
  14. Chris Smith () wrote:
    : Hi,

    : Is there a nicer way of doing the following?

    : $a = lc($a);

    perhaps

    $a = lc $a;

    using $_ you can reduce that somewhat

    $_ = lc;


    but I'm afraid you *can't* say what I often want to say, which is

    while (<>)
    { chomp;
    lc;
     
    Malcolm Dew-Jones, Oct 2, 2003
    #14
  15. Chris Smith

    Chris Smith Guest

    Chris Smith, Oct 4, 2003
    #15
  16. Chris Smith

    Matija Papec Guest

    X-Ftn-To: Chris Smith

    Chris Smith <> wrote:
    >> $a = "\L$a"; #also fine

    >
    >Thank you - just what I was after!


    Don't thank me, if you're really lazy notice that lc $a is still shorter
    version. :)


    --
    Matija
     
    Matija Papec, Oct 4, 2003
    #16
  17. Chris Smith

    Chris Smith Guest

    Matija Papec wrote:

    > X-Ftn-To: Chris Smith
    >
    > Chris Smith <> wrote:
    >>> $a = "\L$a"; #also fine

    >>
    >>Thank you - just what I was after!

    >
    > Don't thank me, if you're really lazy notice that lc $a is still shorter
    > version. :)


    Yes but in the context I need it in, it's lazier ;-)

    --
    Chris Smith
    http://www.infinitemonkeys.org.uk/
     
    Chris Smith, Oct 4, 2003
    #17
    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. Markus Rosenstihl

    Nicer way of strip and replace?

    Markus Rosenstihl, Oct 11, 2005, in forum: Python
    Replies:
    6
    Views:
    400
    Markus Rosenstihl
    Oct 12, 2005
  2. Stefan Arentz

    Is there a nicer way to do this?

    Stefan Arentz, Oct 4, 2007, in forum: Python
    Replies:
    12
    Views:
    467
    Victor B. Gonzalez
    Oct 5, 2007
  3. News123
    Replies:
    3
    Views:
    3,707
    Paul McGuire
    Jul 14, 2010
  4. S Kanakakorn
    Replies:
    3
    Views:
    405
    Jules
    Feb 9, 2007
  5. Remco Hh
    Replies:
    17
    Views:
    185
    Jesse Edelstein
    Nov 21, 2007
Loading...

Share This Page