Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dearest monks,

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
sub seq1 { my $r; my $r0; my @R; for my $c (split '', shift) { unless (defined $r) { $r = $c; $r0 = $c; } elsif($c eq $r0) { $r .= $c; } else { push @R, $r; $r = $c; $r0 = $c; } } push @R, $r if length $r; @R; }
But what is with Perl's promise of "simple things easy"?

Let's look at bigger shells then: Regexes:

sub seq2 { my @x = shift =~ m/((.)\2*)/g; map $x[2*$_], 0..@x/2-1; }
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.

In reply to Elegant way to split into sequences of identical chars? by pKai

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (7)
    As of 2020-01-24 04:17 GMT
    Find Nodes?
      Voting Booth?