my @a = split/:/,$str; @a % 2 && die; # not an even number of items my $r = int(rand(100)); my ($adId, $p); while (($p,$adId) = splice @a,0,2) { if ($r < $p) {last;} $r -= $p; }