# How to call the remains of a pattern match

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

1. ### Jure SimsicGuest

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

2. ### Mark ClementsGuest

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

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 )

--
Perl programming
Fort Worth, Texas

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.

--
Perl programming
Fort Worth, Texas

5. ### Jure SimsicGuest

> 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

Jure Simsic <> 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.

--
Perl programming
Fort Worth, Texas

7. ### Ilya ZakharevichGuest

[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