Velocity Reviews > Perl > How to call the remains of a pattern match

# How to call the remains of a pattern match

Jure Simsic
Guest
Posts: n/a

 04-20-2005
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

Mark Clements
Guest
Posts: n/a

 04-20-2005
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

Guest
Posts: n/a

 04-20-2005
Jure Simsic <(E-Mail Removed)> 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 )

--
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas

Guest
Posts: n/a

 04-20-2005
Jim Gibson <(E-Mail Removed)> wrote:
> In article <Fzv9e.11590\$(E-Mail Removed)>, 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.

--
(E-Mail Removed) Perl programming
Fort Worth, Texas

Jure Simsic
Guest
Posts: n/a

 04-21-2005

> Jim Gibson <(E-Mail Removed)> wrote:
> > In article <Fzv9e.11590\$(E-Mail Removed)>, 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

Guest
Posts: n/a

 04-21-2005
Jure Simsic <(E-Mail Removed)> wrote:
>
>> Jim Gibson <(E-Mail Removed)> wrote:
>> > In article <Fzv9e.11590\$(E-Mail Removed)>, 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.

--
(E-Mail Removed) Perl programming
Fort Worth, Texas

Ilya Zakharevich
Guest
Posts: n/a

 04-22-2005
[A complimentary Cc of this posting was sent to
Jure Simsic
<(E-Mail Removed)>], who wrote in article <Fzv9e.11590\$(E-Mail Removed)>:
> \$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