Re: why the loop-break after pattern-matching



On 01/01/12 08:08, dn.perl@xxxxxxxxx wrote:

Well, apparently in a scalar context, the 'g' option remembers where
it had left off the pattern-matching the last time and takes it up
from that point. !!!

If I had a pound for every time the very act of asking for help on a problem prompts one to find the solution ...

But I wouldn't code "g" at all in this case (unless your real data contains multiple copies of the pattern), and my practice is to use $1 (and $2, $3 etc if necessary) to collect the results of the matches rather than $&. Also, what are you going to do if your pattern doesn't match?

I amused myself on this Neew Year's morning by re-writing your code a little; I'm not an experienced Perl coder so having made suggestions to you I'm open to them myself.

#!/usr/bin/perl
use strict;
use warnings;

my $counter = 0;
while ( my $line = <DATA> ) {
$counter++;
if ( $line =~ /pattern( \S+-\d+)+/i ) {
print "Log is $1\n";
}
else {
chomp $line;
warn "Bad input line no $counter: '$line'\n";
}
}
print "$counter lines read\n";

__DATA__
[pattern number-405] check
Rubbish
[pattern number-406] check
[pattern number-407] check


--

Henry Law Manchester, England
.



Relevant Pages

  • Re: Regular expression help, please!
    ... Thank you, Steve, for the detailed explanation. ... no spaces between the letters and the pound signs. ... >> the pound signs in both the string expression and the pattern with, say, ... >> boundaries? ...
    (microsoft.public.scripting.vbscript)
  • Re: Anyone know a good source for water pump O rings?
    ... eg Vauxhall round one about a pound! ... Assuming that it would fit a pattern part. ... It turns out that Eurocarparts sell them. ...
    (uk.rec.cars.maintenance)
  • Re: Anyone know a good source for water pump O rings?
    ... eg Vauxhall round one about a pound! ... Assuming that it would fit a pattern part. ...
    (uk.rec.cars.maintenance)
  • Re: 2 reasons my 5 ball is progressing well
    ... Because they both train diferent things. ... it will encourage a more consistent pattern. ... As to how much to practice. ... variety than just the 5 ball cascade. ...
    (rec.juggling)
  • Re: 2 reasons my 5 ball is progressing well
    ... >1) Practice clean- very important, I just recently learned from John ... >pattern off balance. ... usual trainers or bare feet. ... first dozen or so throws being consistently lower the ...
    (rec.juggling)