$index = (sort{$$b[1]-$$a[1]}map{[$i++,$#$_]}@aoa)[0]->[0];
That's nice, very nice indeed, blakem ... but some people seem to have problems understanding it, so allow me to take this apart a bit:
  1. @tmp = map {[$i++,$#$_]} @aoa;This creates a list of two element arrays (yes, to be precise of references to two element arrays). The first being the position (index) in @aoa, the second the last index of the current array in @aoa. So for the example given, this produces the following array: ( [0,2], [1,4], [2,7], [3,3], [4,5] )
  2. @sorted = sort {$$b[1]-$$a[1]} @tmp;This created list is now sorted on the 2nd (last index) element, with the biggest element sorted to the head of the list. $$b[1]-$$a[1] is just a fancy way of writing $$b[1] <=> $$a[1] which might look more familiar. So the list now looks like this: ( [2,7], [4,5], [1,4], [3,3], [0,2] )
  3. $index = $sorted[0]->[0];As a last step, the first element of the sorted list is taken (i.e. [2,7] in our example, a reference to a 2 element array) and the first (->[0]) item is extracted. This is the index we were looking for.

-- Hofmator


In reply to Re4: Getting last valid index from a particular list in a @LoL by Hofmator
in thread Getting last valid index from a particular list in a @LoL by willick

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.