Newbie Q - Nicer way to lc something?

C

Chris Smith

Hi,

Is there a nicer way of doing the following?

$a = lc($a);

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

Cheers,
 
T

Tore Aursand

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. :)
 
G

Gunnar Hjalmarsson

Chris said:
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.
 
C

Chris Smith

Tore said:
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 :/
 
C

Chris Smith

Gunnar said:
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".
 
T

Tad McClellan

Chris Smith said:
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
 
U

Uri Guttman

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
 
T

Tore Aursand

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.
 
S

Steve Grazzini

Tad McClellan said:
$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" ?
 
M

Matija Papec

X-Ftn-To: Chris Smith

Chris Smith said:
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
 
J

Jay Tilton

: 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."
 
M

Malcolm Dew-Jones

Chris Smith ([email protected]) 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;
 
M

Matija Papec

X-Ftn-To: Chris Smith

Chris Smith said:
Thank you - just what I was after!

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

Chris Smith

Matija said:
X-Ftn-To: Chris Smith



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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top