in reply to Simple regex question

Scan string.....

use Data::Dumper; my $str = "aaabbccccdd eee"; my $last = ''; my @res; my $i = -1; for my $chr ( split //, $str ) { $i++ unless $chr eq $last; $res[$i] .= $chr; $last = $chr; } print Dumper \@res; __DATA__ $VAR1 = [ 'aaa', 'bb', 'cccc', 'dd', ' ', 'eee' ];

OK so it looks like C without the pointers. So shoot me!

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Simple regex question
by Anonymous Monk on Oct 23, 2003 at 13:35 UTC
    Well, I did have something similar in reserve, but wanted to make it a bit more regexpy for a laugh!

    Thanks for the reply

      Why? Less written code == faster => no; some regexp solution is 'cooler' == you say; posting to PM for a 5 line answer in almost any lang is efficient......

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        No I think you get me wrong, my reply wasn't meant to sound flippant.

        I never said less written code == faster, neither did I suggest that using regex was more cool - that's some odd bias of yours I'm afraid.

        Less written code can == easier to read, and easier to read may == easier to maintain. That seems as valid a reason as any, though an equally valid point might be regex != readable - there's no simple answer.

        I would like to learn as much as possible about about perl, and I thought there should be a regex solution that would give a more compact solution - I attempted a solution which didn't quite work, and some of the replies have given me a greater insight into regex.

        I did say thanks for the reply, and the question did have "regex" in the title, so you'll have to forgive me if I can't understand why you're so pissed off - don't worry I won't follow up on this thread and waste any more time.

        Thanks to everyone, including yourself, for the useful answers you've all given.

      Then why watse our time? Why not go: "this does what I want but..." for some inexplicable reason I need TIMTOWDI. Despite the fact that what I have what I need, with no speed justification/benchmark at all, I would like you all to solve a problem I have already solved, for no particular reason, for free, for someone you dont know......

      I am surprised there is less enthusiasm.

      • Solving problems is one thing
      • Solving solved problems is antoher thing
      • Solving problems for someone who then goes on to go..... might possible erode the milk of HK. JM2c.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print