|Problems? Is your data what you think it is?|
Elegant way to split into sequences of identical chars?by pKai (Priest)
|on Nov 29, 2005 at 20:49 UTC||Need Help??|
pKai has asked for the
wisdom of the Perl Monks concerning the following question:
my journey on the road to Perl wisdom has led me to your gates. I'm here to present to you a little innocent problem, which nonetheless gave me some pain in thinking about it.
The task which torments me is the following:
"Given a string, e. g. "xx556xx", split it into an array, where the split strikes between every non-identical characters, i. e. getting qw(xx 55 6 xx) from the example string above."
That seems easy enough. A first approach with a for-loop might look like this
But what is with Perl's promise of "simple things easy"?
Let's look at bigger shells then: Regexes:
The 1st line is actually mine, while credit for the 2nd goes to murphy on the German perl-community board, fixing an oversight by me.
This looks quite good, and Benchmark even suggests, that it has a little edge in performance over the for-driven sub.
Alas! it would be perfect, if no postprocessing was necessary on the match expression result!
Oh enlighted monks, is any such "easy expression for the simple problem" laid down in your holy books?
Footnote: granted that readability and elegance are to a great extend functions of individual perception, I was nonetheless frustrated that I could not dig up an "elegant" 1-liner for the described problem so far.