$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:
- @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] )
- @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] )
- $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