Brovnik has asked for the wisdom of the Perl Monks concerning the following question:
I have a sub which converts e.g.
I'd like to be able to do e.g.
--
Brovnik
Spec: output is comma separated list of run lengths of the 'X' parts._________XX_X_XX_________ => 2,1,2 __________XXXXX__________ => 5 ____XXXX___XXX__XXXXX____ => 4,3,5
The sub is :
Is there a Regex way of doing the last line ?sub template { my $r = shift; $r =~ s/^_+//; $r =~ s/_+$//; $r =~ s/_+/_/g; return join ',', map { length($_) } split(/_/,$r); }
I'd like to be able to do e.g.
But this if course doesn't do what I need.s/(X+)/length($1)/g;
--
Brovnik
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Replacing string with its length in Regex
by japhy (Canon) on Jul 27, 2001 at 20:40 UTC | |
Re: Replacing string with its length in Regex
by bikeNomad (Priest) on Jul 27, 2001 at 20:34 UTC | |
by petral (Curate) on Jul 29, 2001 at 12:28 UTC | |
Re: Replacing string with its length in Regex
by merlyn (Sage) on Jul 27, 2001 at 22:00 UTC | |
by Brovnik (Hermit) on Jul 27, 2001 at 23:07 UTC | |
by Everlasting God (Beadle) on Jul 28, 2001 at 00:17 UTC |
Back to
Seekers of Perl Wisdom