RegExp in one statement

D

Don Stefani

How can I do this in one statement?

my $ml_name = $q->param('ml_name');
$ml_name =~ s/^(\s*)//;

Thanks

- dstefani
 
B

Ben Morrow

Quoth (e-mail address removed):
How can I do this in one statement?

Why do you need to?
my $ml_name = $q->param('ml_name');
$ml_name =~ s/^(\s*)//;

(my $ml_name = $q->param('ml_name')) =~ s/^\s*//;

No need for the parens: you don't use $1.
Are you sure you don't want a /g on there? This will only remove the
first piece of whitespace.

Ben
 
S

Sam Holden

How can I do this in one statement?

my $ml_name = $q->param('ml_name');
$ml_name =~ s/^(\s*)//;

Thanks

(my $ml_name = $q->param('ml_name')) =~ s/^(\s*)//;

But it's ugly and a hard to read and not worth using.
 
K

Kevin Collins

Don Stefani said:
How can I do this in one statement?

my $ml_name = $q->param('ml_name');
$ml_name =~ s/^(\s*)//;
(my $ml_name = $q->param('ml_name')) =~ s/^\s+//;

The parens are not needed since you are doing nothing with the captured match.
Additionally, you only care about removing space chars if there are some, and
since * means "zero or more" it is better to use +, which means "one or more"
because changing nothing to nothing is meaningless...

Kevin
 
D

Don Stefani

Kevin said:
(my $ml_name = $q->param('ml_name')) =~ s/^\s+//;

The parens are not needed since you are doing nothing with the captured match.
Additionally, you only care about removing space chars if there are some, and
since * means "zero or more" it is better to use +, which means "one or more"
because changing nothing to nothing is meaningless...

Kevin


Thanks for the info.
Others seemed to think that this was ugly and hard to read, it works for me.

Thanks to all for you quick replys.

- dstefani
 
K

Kevin Collins

Quoth (e-mail address removed):

Why do you need to?


(my $ml_name = $q->param('ml_name')) =~ s/^\s*//;

No need for the parens: you don't use $1.
Are you sure you don't want a /g on there? This will only remove the
first piece of whitespace.

How many times can you match spaces starting at the beginning of a pattern?
Notice the "^"?

Kevin
 
G

gnari

Don Stefani said:
Kevin said:
Others seemed to think that this was ugly and hard to read, it works for
me.

what do they know? :)

this is a common perl idiom, and as such easily recognizable,
but should only be used if you are comfortable with it, because
you might have to take a look at it in 6 months time. ask
yourself: will i understand it then ? in that case, fine.

gnari
 
G

Gregory Toomey

Abigail said:
Abigail
print v74.117.115.116.32, v97.110.111.116.104.101.114.32,
      v80.101.114.108.32, v72.97.99.107.101.114.10;

One of your better ones!

gtoomey
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top