#!/usr/bin/perl use strict; use warnings; my $range="0-2,6-10,13,15"; my $test="this is a test with more than 15 characters"; for my $r (split (",",$range)) { if ($r =~ /^(\d+)(-(\d+))?$/) { my $first=$1; my $length=1; if (defined ($3)) { $length=$3-$1+1; } print "$r - ", substr ($test, $first,$length),"\n"; } }