Mohammed Khatib wrote:

> Hi All,
> 
> A simple question (but this had me baffled for ages today).
> 
> Take a statement such as the following:
> 
> if (s!($string)!!g)
> {
>     # code
> }
> 
> Since we are using the g modifier in the substitution, is
> there a way to access an array or something which holds all
> the captured matches (assuming there are multiple matches)
> in the global replace? Obviously $1 doesn't work (even if it
> did, it'll probably only give me the most recent match).
> 
> I know that I can use
> 
> while (s!($string)!!)
> {
>     # access to $1 is allowed here
> }
> 
> but I'm curious to know the answer to my question above.


In addition to Ron's comments:

my $string = 'abc';

$_ = "123abc123abc123abc";
print "Before: $_\n";
my @matches;
push @matches, $1 while s/($string)//;  # works to pick up the matches
print scalar @matches, " matches\n";
print "After: $_\n";

foreach (@matches) {
        print "match: $_\n";
}
print "\n";

# or just get the count of matches since we know what we are matching
# on each match

$_ = "123abc123abc123abc";
print "Before: $_\n";
my $matches = (s/($string)//g);
print "$matches matches\n";
print "After: $_\n";

__END__






-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to