in reply to Re: Re: Re: Getting last valid index from a particular list in a @LoL
in thread Getting last valid index from a particular list in a @LoL

$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

Replies are listed 'Best First'.
Re: Re4: Getting last valid index from a particular list in a @LoL
by blakem (Monsignor) on Oct 17, 2001 at 21:35 UTC
    Thanks for expounding on my uncharacteristically terse answer. I was a bit grumpy when I wrote it, having just gone a few rounds over at Varying Variable Names.....

    -Blake