in reply to need help to split list

Well when I started there were no answers but oh well, I'll post my unworthy code anyway.
set_ranged_data("1-5,6a-e,7w-z,10-13"); while(my @enumerated = get_next_range()) { print join("\n", @enumerated); print "\n"; } { my $data; my $pos; sub set_ranged_data { $data = shift; $pos = 0; 1; } sub get_next_range { return unless $data; my $end_pos = index($data, ","); $end_pos = length($data) if ($end_pos) < 0; my $range = substr($data, 0, $end_pos); substr($data, 0, $end_pos + 1) = ""; my @enumerated = _enumerate($range); wantarray ? return @enumerated : return \@enumerated } sub _enumerate { my $range = shift; return ($range) unless index($range, "-") > 0; my ($set, $end_point) = split /-/, $range; my ($begin_lead, $begin_point) = _decompose($set); return map {$begin_lead . $_} ($begin_point .. $end_point); } sub _decompose { my $set = shift; my ($lead, $point) = $set =~ /^(\d+)?(\D)?$/; return ($lead, $point) if ($lead && $point); return ("", $point) if ($point); return ("", $lead) if ($lead); return; } }

Replies are listed 'Best First'.
Re: Re: need help to split list
by bfdi533 (Friar) on Oct 31, 2003 at 19:22 UTC
    Thanks for the code even though there were already answers. This is what the site is all about and the many different ways of doing this helps me to expand my knowledge of the language. Thanks, Ed