Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Elegant way to split into sequences of identical chars?

by tphyahoo (Vicar)
on Nov 30, 2005 at 14:54 UTC ( #512949=note: print w/replies, xml ) Need Help??


in reply to Elegant way to split into sequences of identical chars?

This is almost a repeat of tye's answer, but without peking at the others I came up with
use strict; use warnings; use Data::Dumper; my @repeats; while ( 'xx556xx' =~ m/((.)(\2)*)/g ) { push @repeats, $1; } print Dumper(\@repeats);

Replies are listed 'Best First'.
Re^2: Elegant way to split into sequences of identical chars?
by eric256 (Parson) on Nov 30, 2005 at 16:25 UTC

    Forgot to post my solution early, but its similar to yours so i'll add it here.

    use Data::Dumper; my $test = "xx556xxyyy"; my @matches ; push @matches, $1 while ($test =~ /((.)\2*)/g); print Dumper(@matches);

    These solutions have the added benefit of being able to switch to just a list of chars by changeins $1 to $2. Not sure thats of any use but hey. lol. I'm mildly suprised there is no regex command to match last char, or a way to specify which groups to return and with are just used internal to the regex, I guess the need doesn't arise that often.


    ___________
    Eric Hodges $_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9'; s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2019-05-24 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (147 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!