#!/usr/bin/perl use strict; use warnings; use diagnostics; use Data::Dumper; use Scalar::Util qw(looks_like_number); my $text = '1-20,1,1,1,2,4,5,8,9,3-4'; my $list = &_text_to_list($text); print Dumper $list; exit; sub _text_to_list # { my @list = split( ',', shift ); my %hash; for ( 0 .. $#list ) { if ( $list[$_] =~ /(.*)-(.*)/ ) { $hash{$_} = 1 for $1 .. $2; next; } $hash{ $list[$_] } = 1 if looks_like_number( $list[$_] ); } @list = (); push @list, $_ for keys %hash; return \@list; }