Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Splitting a string

by ikegami (Patriarch)
on Feb 08, 2006 at 16:53 UTC ( [id://528846]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://528846]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found