#!/usr/bin/perl -wl use strict; my @a=0..1e3; # Set higher than any reasonable message number my @list = ("1, 3, 5-7", "1-5, 2, 4", "1-3, 2-5"); for (@list){ my %h; s/-/../g; $h{$_}++ for @a[(eval $_)]; print join " ", sort {$a<=>$b} keys %h; }