in reply to How can I replace space sequences in a string with the number of spaces in each sequence?

Use the /e modifier to include code in the replacement part of the substitution:
$input =~ s/( +)/length $1/ge;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re: How can I replace space sequences in a string with the number of spaces in each sequence?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How can I replace space sequences in a string with the number of spaces in each sequence?
by tkguifan (Scribe) on Feb 04, 2015 at 11:41 UTC
    Ingenious. I thought it has something to do with pack-unpack ( which I fear ).
Re^2: How can I replace space sequences in a string with the number of spaces in each sequence?
by tkguifan (Scribe) on Feb 04, 2015 at 11:48 UTC
    By the way: what's your solution, if you have to do the same, but with 8 character blocks. So decompose the string into 8 character blocks ( it is promised that its length is an integer multiple of 8 ), and make the substitution within each block, the blocks separated by '/'.
    my $input = 'r k rpp pp'; my $output = 'r3k2r/pp4pp';

      I would nest regexes by calling a function on each 8block.

      How would you implement it ?

      update

      Or

    • split 8 blocks to array
    • replace whitespace for each element
    • join array with separator

      Can be done in a one liner with map .

      Cheers Rolf

      PS: Je suis Charlie!

        This is tested:
        my $fen=join('/', map { $_=~s/( +)/length $1/ge; $_; } $self->{rep}=~/.{8}/g );