Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pos and capture

Reply
Thread Tools

pos and capture

 
 
UToronto News
Guest
Posts: n/a
 
      08-10-2004

I have a question about capturing information from a regular expression
match

when i run

$string="the rain in spain falls mainly on the plain";
$searchpattern=".ain";

while ($string=~m/$searchpattern/g)
{
$position=pos($string)-length($search);
print "position=$position\n";
pos($string)=$position+1;
}

-- this searches for all occurences of $search in $string, and prints out
the starting position of each
position=8
position=17
position=28
position=43


but when i run
while (($capture)=$string=~m/($searchpattern)/g)
{
$position=pos($string)-length($searchpattern);
print "position=$position : $capture\n";
pos($string)=$position+1;
}

-- I am able to capture the match, but pos($string) is empty, resulting in
$position equal to 0-length($capture)
position=-4 : rain

When capturing the match, does the pos pointer reset? Is there a way of
using this pointer with capture?

Thanks
Larry



 
Reply With Quote
 
 
 
 
LEH
Guest
Posts: n/a
 
      08-10-2004
I sorted out how to get this working.
instead of capturing into a variable, I capture into the $1 and use that

ie
while($string=~/($searchpatten)/g)
{
$position=pos($string)-length($searchpattern);
print "position=$position: $1\n";
pos($string)=$position+1;
}

this does work, but I am still curious to know why my capturing into a
variable resets pos($string)

Larry








"UToronto News" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> I have a question about capturing information from a regular expression
> match
>
> when i run
>
> $string="the rain in spain falls mainly on the plain";
> $searchpattern=".ain";
>
> while ($string=~m/$searchpattern/g)
> {
> $position=pos($string)-length($search);
> print "position=$position\n";
> pos($string)=$position+1;
> }
>
> -- this searches for all occurences of $search in $string, and prints out
> the starting position of each
> position=8
> position=17
> position=28
> position=43
>
>
> but when i run
> while (($capture)=$string=~m/($searchpattern)/g)
> {
> $position=pos($string)-length($searchpattern);
> print "position=$position : $capture\n";
> pos($string)=$position+1;
> }
>
> -- I am able to capture the match, but pos($string) is empty, resulting in
> $position equal to 0-length($capture)
> position=-4 : rain
>
> When capturing the match, does the pos pointer reset? Is there a way of
> using this pointer with capture?
>
> Thanks
> Larry
>
>
>



 
Reply With Quote
 
 
 
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      08-10-2004
[posted & mailed]

On Tue, 10 Aug 2004, LEH wrote:

>this does work, but I am still curious to know why my capturing into a
>variable resets pos($string)
>
>> while (($capture)=$string=~m/($searchpattern)/g)


The problem is that saying

(...) = $string =~ /pattern/g;

enforces *list* context on the pattern match, and a global pattern match
in list context matches as many times as possible, and afterwards, pos()
doesn't have a useful value.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
RPI Corporation Secretary % have long ago been overpaid?
http://japhy.perlmonk.org/ %
http://www.perlmonks.org/ % -- Meister Eckhart


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-11-2004
UToronto News <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> I have a question about capturing information from a regular expression
> match
>
> when i run


....without strictures and warnings, it seems.

> $string="the rain in spain falls mainly on the plain";
> $searchpattern=".ain";
>
> while ($string=~m/$searchpattern/g)
> {
> $position=pos($string)-length($search);

^^^^^^^
There is no variable "$search". "strict" would have told you so.
Also, the length of the search pattern is generally not the same
as the length of the match. Use the @- array for the beginning
of a match.

> print "position=$position\n";
> pos($string)=$position+1;


This is an error. pos( $string) points already one character past
the match. Incrementing it further may miss some.

> }
>
> -- this searches for all occurences of $search in $string, and prints out
> the starting position of each
> position=8
> position=17
> position=28
> position=43


Have you checked that? Your positions are too large by 4:

print "position=$-[ 0]\n" while $string =~ /$searchpattern/g;

position=4
position=13
position=24
position=39

Your question about pos() after a match in list context has been answered.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bit check using function check(var,pos) ((var)&(1<<pos)) prati C Programming 0 10-27-2012 05:25 PM
absolute pos. layer and css inline/and external se Javascript 19 08-18-2010 10:41 PM
SuperVideoCap work as a broadcast capture and screen capture and record tool. hely0123 Media 0 10-30-2007 08:59 AM
perl bug with pos() and placing matches in an array? t13_e@yahoo.com Perl Misc 6 03-17-2005 04:55 AM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments