my $str="17:43:33:21:23:19:27:6"; my @a = split/:/,$str; @a % 2 && die; # not an even number of items while (my ($p,$ad) = splice @a,0,2) { push @ary, ($ad) x $p; } my $adId=$ary[int(rand(100))];