in reply to need help to split list

As a one-liner

@a=map{m[(\d+)([a-z]?)-(\d+|[a-z])]and$2?(map{$1.$_}$2..$3):($1..$3)}s +plit',',$s; print "@a"; 1 2 3 4 5 6a 6b 6c 6d 6e 10 11 12 13

Update: The above completely misses the 7!

Corrected and expanded for a little more clarity

print map{ m[ (\d+) ([a-z]?) -? (\d+|[a-z])? ]x and $2 ? ( map{ $1 . $_ } $2 .. $3 ) : $3 ? ( $1 .. $3 ) : ( $1 ) }split ',', $s; 1 2 3 4 5 6a 6b 6c 6d 6e 7 10 11 12 13

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!

Replies are listed 'Best First'.
Re: Re: need help to split list
by sgifford (Prior) on Oct 24, 2003 at 22:21 UTC

    Slick solution, but it skips 7. Here's a fixed (and slightly more readable, using if instead of the ternary operator) version.

    #!/usr/bin/perl -l my $s = '1-5,6a-e,7,10-13'; my @a = map{ if (m[ (\d+) ([a-z]?) - (\d+|[a-z]) ]x) { if ($2) { map{ $1 . $_ } $2 .. $3; } else { ( $1 .. $3 ); } } else { $_; } } split ',', $s; print join ' ', @a;
Re: Re: need help to split list
by Anonymous Monk on Oct 25, 2003 at 10:49 UTC

    ... and the logical conclusion:

    @a=map/(\d+)(\D*)-/?$2?map$1.$_,$2..$':$1.$2..$':$_,split',',$s;