How to call the remains of a pattern match

Discussion in 'Perl Misc' started by Jure Simsic, Apr 20, 2005.

  1. Jure Simsic

    Jure Simsic Guest

    Let's say I have code like this:

    $a="a:1 b:3 c:foo d:4";

    while ($a =~ m/(\w):(\w+)/g){
    # do stuff with $1 and $2
    if ($2 eq "foo"){
    &sub( ${what's left of $a} )
    }
    }

    One way that I can think of is
    $a =~ s/^.*$2//;
    before calling &sub($a), but that seems awkward..

    Is there any nicer way to achieve this?

    Tnx
    Jure
     
    Jure Simsic, Apr 20, 2005
    #1
    1. Advertising

  2. Jure Simsic wrote:
    > Let's say I have code like this:
    >
    > $a="a:1 b:3 c:foo d:4";
    >
    > while ($a =~ m/(\w):(\w+)/g){
    > # do stuff with $1 and $2
    > if ($2 eq "foo"){
    > &sub( ${what's left of $a} )
    > }
    > }
    >
    > One way that I can think of is
    > $a =~ s/^.*$2//;
    > before calling &sub($a), but that seems awkward..
    >
    > Is there any nicer way to achieve this?
    >
    > Tnx
    > Jure


    Use pos and substr.

    bob 1297 $ cat testre.pl
    use strict;
    use warnings;

    my $testString = shift;
    while($testString=~/(.)/g){
    my $lastPos = pos $testString;
    my $remainder = substr($testString,$lastPos);
    print "$1 => $remainder\n";
    }

    bob 1298 $ perl testre.pl mark
    m => ark
    a => rk
    r => k
    k =>

    You could use $' (look for POSTMATCH in man perlre, but this imposes a
    performance penalty).

    Mark
     
    Mark Clements, Apr 20, 2005
    #2
    1. Advertising

  3. Jure Simsic <> wrote:


    > $a="a:1 b:3 c:foo d:4";
    >
    > while ($a =~ m/(\w):(\w+)/g){



    while ($a =~ s/^(\w):(\w+)\s*// ){


    > # do stuff with $1 and $2
    > if ($2 eq "foo"){



    &sub( $a )


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 20, 2005
    #3
  4. Jim Gibson <> wrote:
    > In article <Fzv9e.11590$>, Jure Simsic


    >> &sub( ${what's left of $a} )



    > 2. There is no need to put ampersand (&) in front of function call.



    Unless you've been so silly as to use a reserved word as the
    function name, like this OP did.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 20, 2005
    #4
  5. Jure Simsic

    Jure Simsic Guest

    Tad McClellan wrote:

    > Jim Gibson <> wrote:
    > > In article <Fzv9e.11590$>, Jure Simsic

    >
    > >> &sub( ${what's left of $a} )

    >
    >
    > > 2. There is no need to put ampersand (&) in front of function call.

    >
    >
    > Unless you've been so silly as to use a reserved word as the
    > function name, like this OP did.


    In fact I didn't. It was just a code example, to make it as clear as
    possible..
    Thanx to everyone..

    Jure
     
    Jure Simsic, Apr 21, 2005
    #5
  6. Jure Simsic <> wrote:
    > Tad McClellan wrote:
    >
    >> Jim Gibson <> wrote:
    >> > In article <Fzv9e.11590$>, Jure Simsic

    >>
    >> >> &sub( ${what's left of $a} )

    >>
    >>
    >> > 2. There is no need to put ampersand (&) in front of function call.

    >>
    >>
    >> Unless you've been so silly as to use a reserved word as the
    >> function name, like this OP did.

    >
    > In fact I didn't.



    Yes you did, it is quoted above for all to see.


    > It was just a code example,



    It was a code example that used a reserved word as the function name.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 21, 2005
    #6
  7. [A complimentary Cc of this posting was sent to
    Jure Simsic
    <>], who wrote in article <Fzv9e.11590$>:
    > $a="a:1 b:3 c:foo d:4";
    >
    > while ($a =~ m/(\w):(\w+)/g){
    > # do stuff with $1 and $2
    > if ($2 eq "foo"){
    > &sub( ${what's left of $a} )
    > }
    > }
    >
    > One way that I can think of is
    > $a =~ s/^.*$2//;
    > before calling &sub($a), but that seems awkward..


    > Is there any nicer way to achieve this?


    while ($a =~ m/(\w):(\w+)(?=(.*))/gs){ # Note the /s modifier
    # do stuff with $1 and $2
    if ($2 eq "foo"){
    &sub( $3 )
    }
    }

    Hope this helps,
    Ilya
     
    Ilya Zakharevich, Apr 22, 2005
    #7
    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. vMike
    Replies:
    1
    Views:
    482
    Natty Gur
    Aug 26, 2003
  2. vMike
    Replies:
    1
    Views:
    355
    Natty Gur
    Aug 26, 2003
  3. Chris
    Replies:
    1
    Views:
    311
    bruce barker
    Apr 12, 2004
  4. \Rob\
    Replies:
    0
    Views:
    425
    \Rob\
    May 10, 2004
  5. Tim Marsden
    Replies:
    11
    Views:
    968
    Steven Cheng[MSFT]
    Jun 3, 2004
Loading...

Share This Page