in reply to Splitting a string

Finally, one line, and no experimental features!

sub double_it { return join ' ', $_[0] =~ /\w(?=\w(?<=(..)))/g; }

Alternate versions:

sub double_it { local ($_) = @_; (my @chars1) = (my @chars2) = split //; pop @chars1; shift @chars2; return join ' ', grep { !/\s/ } map { $chars1[$_] . $chars2[$_] } 0..$#chars1; }
sub double_it { local ($_) = @_; my @list = /(\w)(?=(\w))/g; my @pairs; push(@pairs, shift(@list).shift(@list)) while @list; return join(' ', @pairs); }
sub double_it { our @pairs; local *pairs; () = $_[0] =~ /(\w)(?=(\w))(?{ push(@pairs, "$1$2"); })/g; return join(' ', @pairs); }

Replies are listed 'Best First'.
Re^2: Splitting a string
by nobull (Friar) on Feb 08, 2006 at 18:32 UTC
    /\w(?=\w(?<=(..)))/g;

    Isn't that rather more complicated than necessary?

    /(?=(\w\w))./g;
        It is true that /PATTERN/g will DWIM when /PATTERN/ matches the empty string and not find an infinite number of matches as the same point.

        I, however, consider code that relies on this behaviour in order save one character to be less simple.