in reply to Elegant way to split into sequences of identical chars?
I know, obfuscated != elegant. Nevertheless, I thought it was kind of neat.
Caution: Contents may have been coded under pressure.
The trick with $| is that it's a toggle, so grep throws away every other member of its input. The same thing could be done with a normal variable like so:my $str = 'xx556xx'; my @x = grep --$|, $str =~ m/((.)\2*)/g; print join ',', @x;
or using split:my $str = 'xx556xx'; my $tog = 0; my @x = grep $tog = !$tog, $str =~ m/((.)\2*)/g; print join ',', @x;
my $str = 'xx556xx'; my $tog = 0; my @x = grep $tog = !$tog, split /(?<=(.))(?!\1)/, $str; print join ',', @x;
Caution: Contents may have been coded under pressure.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Elegant way to split into sequences of identical chars?
by bioMan (Beadle) on Nov 29, 2005 at 23:13 UTC | |
by QM (Parson) on Nov 30, 2005 at 02:46 UTC |
In Section
Seekers of Perl Wisdom