Help for this page

Select Code to Download


  1. or download this
    my @items = (
        ["foo", 1],
    ...
        ["baz", 4],
        ["quux", 9]
    );
    
  2. or download this
      use List::Util;
      my $total_weight = List::Util::sum(map { $_->[1] } @items);
    
  3. or download this
      my $pick = int(rand($total_weight));
      my $picked_item = (List::Util::first { $pick -= $_->[1]; $pick < 0 }
    + @items)->[0];