This is a variant on duff's method that's a bit faster, and I have here performance numbers to demonstrate it:
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; }
Now, the performance numbers; it's pretty easy to test all the variants given so far with Benchmark.pm:
Rate origCode duff QM L~R ccn fizbin origCode 4029/s -- -34% -77% -82% -87% -89% duff 6137/s 52% -- -65% -73% -81% -83% QM 17360/s 331% 183% -- -24% -46% -53% L~R 22908/s 469% 273% 32% -- -29% -38% ccn 32106/s 697% 423% 85% 40% -- -12% fizbin 36690/s 811% 498% 111% 60% 14% --
I have the benchmark code, but it's basically just a bunch of cuts and pastes of the code that's been posted on this thread, so I'll only post it if asked.

Update: Fixed oops in code, updated performance figures.

-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

In reply to Re: $str to %hash to @ary by fizbin
in thread $str to %hash to @ary by abaxaba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.