RegExp in one statement

Discussion in 'Perl Misc' started by Don Stefani, May 11, 2004.

  1. Don Stefani

    Don Stefani Guest

    How can I do this in one statement?

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

    Thanks

    - dstefani
     
    Don Stefani, May 11, 2004
    #1
    1. Advertising

  2. Don Stefani

    Ben Morrow Guest

    Quoth :
    > 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

    --
    'Deserve [death]? I daresay he did. Many live that deserve death. And some die
    that deserve life. Can you give it to them? Then do not be too eager to deal
    out death in judgement. For even the very wise cannot see all ends.'
     
    Ben Morrow, May 11, 2004
    #2
    1. Advertising

  3. Don Stefani

    Sam Holden Guest

    On Tue, 11 May 2004 21:58:23 GMT, Don Stefani <> wrote:
    > 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.

    --
    Sam Holden
     
    Sam Holden, May 11, 2004
    #3
  4. In article <30coc.47437$>, Don Stefani
    wrote:
    > 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
     
    Kevin Collins, May 11, 2004
    #4
  5. Don Stefani

    Don Stefani Guest

    Kevin Collins wrote:
    > In article <30coc.47437$>, Don Stefani
    > wrote:
    >
    >>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



    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
     
    Don Stefani, May 11, 2004
    #5
  6. In article <c7ring$lif$>, Ben Morrow wrote:
    >
    > Quoth :
    >> 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.


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

    Kevin
     
    Kevin Collins, May 11, 2004
    #6
  7. Don Stefani

    gnari Guest

    "Don Stefani" <> wrote in message
    news:xrcoc.47447$...
    > Kevin Collins wrote:
    > > In article <30coc.47437$>, Don

    Stefani
    > > wrote:
    > >
    > >>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+//;
    > >


    > 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
     
    gnari, May 12, 2004
    #7
  8. Abigail wrote:

    > 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
     
    Gregory Toomey, May 12, 2004
    #8
    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. Greg Hurrell
    Replies:
    4
    Views:
    164
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    491
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    194
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    181
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page