"zhihua li" schreef:

> I'm curious if there's any smart code to calculate the "distance"
> between the matches in a text.
> Suppose I have a text like this:
> syhk...yes...uhg;ka=...yes...yiealg.....yes.......ghe;a...yes...
> Apparently it has multiple words of "yes".
>  I'd like to know how many characters there are between the first and
> the last "yes". The way I now come up with to do this is to use
> substitution to "mark" the first and the last match, then use a
> counting loop to calculate the characters......rather straightforward
> and stupid method.
>
> Anyone has a better idea about that?


If the parts have some meaning later on, I would use a capturing split:

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

  { local ($\, $") = ("\n", '') ;

    while (<DATA>)
    {
      @_ = split /(yes)/ ;   #  <-- the meat
      print length "@_[2 .. $#_-2]" ;
    }
  }

__DATA__
..yes......yes....yes..........yes...
yes......yes....yes..........yes...
yes......yes....yes..........yes

This depends on the last 'yes' not being at the very end of the string,
as is the case with the DATA-approach, because then there is always at
least a newline following it.

-- 
Affijn, Ruud

"Gewoon is een tijger."


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to