in reply to need help to split list

Another method, one liner using two splits:

use strict; use Data::Dumper; my $list = "1-5,6a-e,7,9-12,89xDC-G"; my @s = map { my @t = split /-/; my $l = length($t[0]) - 1; # length less 1 $#t == 0 ? $_ : $t[0] + 0 eq $t[0] ? # pure number? $t[0] .. $t[1] : map { substr($t[0],0,$l) . $_ } substr($t[0],$l,1) .. $t[1] } split /,/, $list; print Dumper(\@s);
And the output is:
$VAR1 = [ 1, 2, 3, 4, 5, '6a', '6b', '6c', '6d', '6e', '7', 9, 10, 11, 12, '89xDC', '89xDD', '89xDE', '89xDF', '89xDG' ];
Update: Typo fixed - 89xDC-G (was 89xFC-G). Thanks OverlordQ!

Replies are listed 'Best First'.
Re: Re: need help to split list
by OverlordQ (Hermit) on Oct 26, 2003 at 07:40 UTC
    Edit: edit of course it's typo *grin*
    my $list = "1-5,6a-e,7,9-12,89xFC-G";
    and then
              '89xDC',
              '89xDD',
              '89xDE',
              '89xDF',
              '89xDG'
    
    Did I miss something, or was it a typo? (89xFC changed to 89xDC)
      Thanks for catching my typo! Yes I ran the code with 89xDC-G, and when I posted the code, I pasted first, and then decided to try some new data, and instead of cut and paste the modified code again, just editted by hand...