in reply to Question on s///eg

Assuming you're using 5.6.0 or greater, you can use a range generator that japhy put together: List-to-Range generation. The rest should just be details :)

my @channels = (1, 1, 0, 1, 1, 1, 0); my @vals = map { $_ + 1 } grep { $channels[$_] } 0 .. $#channels; my $val = num2range( @vals ); print $val, $/; sub num2range { local $_ = join ',' => sort { $a <=> $b } @_; s/(?<!\d)(\d+)(?:,((??{$++1})))+(?!\d)/$1-$+/g; return $_; }

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.