abaxaba,
- Repeatedly doing hash lookups is slower than doing it once
- The order appears reversed since as is you can't have two ads with the same weight nor lookup the weight of an add.
Assuming it is ok to reverse the order, I have presented the following unique solution that does not require an array:
#!/usr/bin/perl
use strict;
use warnings;
my $lookup;
my %hash = split /:/ , '43:17:21:33:19:23:6:27';
while (my ($key, $val) = each %hash) {
$lookup .= pack("C*", ($key) x $val);
}
print unpack("C", substr($lookup, rand 100, 1));