Home > Archive > PERL Beginners > October 2006 > count the characters between the matches
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
count the characters between the matches
|
|
| Zhihua Li 2006-10-23, 9:57 pm |
| hi netters,
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?
Thanks a lot!
________________________________________
_________________________
Ãâ·ÑÏÂÔØ MSN Explorer: http://explorer.msn.com/lccn
| |
| Hou Bou 2006-10-23, 9:57 pm |
| How about this .
$str =3D =
'syhk...yes...uhg;ka=3D...yes...yiealg.....yes.......ghe;a...yes...';
$count =3D $str;
$count =3D $count =3D~ s/yes//g;
print "$count\n"
-----Original Message-----
From: zhihua li [mailto:lzhtom@hotmail.com]
Sent: Tuesday, October 24, 2006 10:05 AM
To: beginners@perl.org
Subject: count the characters between the matches
hi netters,
I'm curious if there's any smart code to calculate the "distance" =
between=20
the matches in a text.
Suppose I have a text like this: =20
syhk...yes...uhg;ka=3D...yes...yiealg.....yes.......ghe;a...yes...
Apparently it has multiple words of "yes".=20
I'd like to know how many characters there are between the first and =
the=20
last "yes". The way I now come up with to do this is to use substitution =
to=20
"mark" the first and the last match, then use a counting loop to =
calculate=20
the characters......rather straightforward and stupid method.
Anyone has a better idea about that?
Thanks a lot!
________________________________________
_________________________
=C3=E2=B7=D1=CF=C2=D4=D8 MSN Explorer: http://explorer.msn.com/lccn =20
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| John W. Krahn 2006-10-23, 9:57 pm |
| zhihua li wrote:
> hi netters,
Hello,
> 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".
$ perl -le'
my $string = q/syhk...yes...uhg;ka=...yes...yiealg.....yes.......ghe;a...yes.../;
my $lookup = q/yes/;
my $last = rindex $string, $lookup;
my $first = index $string, $lookup;
print $last - ( $first + length $lookup ) if $last >= 0 && $first >= 0;
'
48
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Chris Charley 2006-10-23, 9:57 pm |
|
----- Original Message -----
From: ""zhihua li"" <lzhtom@hotmail.com>
Newsgroups: perl.beginners
To: <beginners@perl.org>
Sent: Monday, October 23, 2006 9:05 PM
Subject: count the characters between the matches
> hi netters,
>
> 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?
>
> Thanks a lot!
This will give the result you want.
#!/usr/bin/perl
use strict;
use warnings;
$_ = 'syhk...yes...uhg;ka=...yes...yiealg.....yes.......ghe;a...yes...';
if (/.*?(yes).*(yes)/) {
print "position at end of first yes is $+[1]\n";
print "position at beginning of last yes is $-[2]\n";
print "number of characters between first and last is ", $-[2] -
($+[1] -1 );
}
**prints
position at end of first yes is 10
position at beginning of last yes is 58
number of characters between first and last is 49
Chris
| |
| Paul Lalli 2006-10-23, 9:57 pm |
| Zhihua Li wrote:
> 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?
You've already gotten three completely different suggestions, so here's
a fourth:
my $str =
'syhk...yes...uhg;ka=...yes...yiealg.....yes.......ghe;a...yes...';
$str =~ /yes(.*)yes/;
print length $1;
Paul Lalli
| |
| Jeff Pang 2006-10-23, 9:57 pm |
|
> 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.
I think there are some ways to do that,I'll give my way:
$ perl -le 'my $s="syhk...yes...uhg;ka=...yes...yiealg.....yes.......ghe;a...yes...";my ($got)=$s=~/yes(.*)yes/;print length($got)'
48
Hope it helps.
--
Books below translated by me to Chinese.
Practical mod_perl: http://home.earthlink.net/~pangj/mod_perl/
Squid the Definitive Guide: http://home.earthlink.net/~pangj/squid/
| |
| Rob Dixon 2006-10-24, 3:56 am |
| zhihua li wrote:
> hi netters,
>
> 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?
>
> Thanks a lot!
Hi.
If you use a regex to capture the characters between the yeses you can just take
the length of the capture:
my $text = q(syhk...yes...uhg;ka=...yes...yiealg.....yes.......ghe;a...yes...);
$text =~ /yes(.*)yes/;
my $nchars = length $1;
print $nchars, "\n";
The answer's 48!
HTH,
Rob
| |
| Dr.Ruud 2006-10-24, 3:56 am |
| "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."
|
|
|
|
|