Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   How to call the remains of a pattern match (http://www.velocityreviews.com/forums/t891923-how-to-call-the-remains-of-a-pattern-match.html)

 Jure Simsic 04-20-2005 04:37 PM

How to call the remains of a pattern match

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 04-20-2005 05:08 PM

Re: How to call the remains of a pattern match

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

Re: How to call the remains of a pattern match

Jure Simsic <jure.simsic@mobitel.si> 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 )

--
Fort Worth, Texas

Re: How to call the remains of a pattern match

Jim Gibson <jgibson@mail.arc.nasa.gov> wrote:
> In article <Fzv9e.11590\$F6.2306405@news.siol.net>, 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.

--
Fort Worth, Texas

 Jure Simsic 04-21-2005 06:52 AM

Re: How to call the remains of a pattern match

> Jim Gibson <jgibson@mail.arc.nasa.gov> wrote:
> > In article <Fzv9e.11590\$F6.2306405@news.siol.net>, 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

Re: How to call the remains of a pattern match

Jure Simsic <jure.simsic@mobitel.si> wrote:
>
>> Jim Gibson <jgibson@mail.arc.nasa.gov> wrote:
>> > In article <Fzv9e.11590\$F6.2306405@news.siol.net>, 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.

--
Fort Worth, Texas

 Ilya Zakharevich 04-22-2005 09:23 AM

Re: How to call the remains of a pattern match

[A complimentary Cc of this posting was sent to
Jure Simsic
<jure.simsic@mobitel.si>], who wrote in article <Fzv9e.11590\$F6.2306405@news.siol.net>:
> \$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

 All times are GMT. The time now is 11:18 AM.