in reply to Find last position of an element in array

skipping past the sigil or brace mismatch, here is my five minute contribution...

perl -le ' my @arr = (1,1,1,2,2,2,2,2,3,3,3,4,4,4,4,4,4,5,5,6); print (@arr - index((reverse(@arr)), 4)); ' __output__ 17

Replies are listed 'Best First'.
Re^2: Find last position of an element in array
by kcott (Archbishop) on Mar 29, 2022 at 21:49 UTC

    G'day dbuckhal,

    17 = off-by-one error. Fix: use $#arr instead of @arr.

    $ perl -le ' my @arr = (1,1,1,2,2,2,2,2,3,3,3,4,4,4,4,4,4,5,5,6); print ($#arr - index((reverse(@arr)), 4)); print "@arr[16, 17]"; ' 16 4 5

    — Ken

Re^2: Find last position of an element in array
by hippo (Archbishop) on Mar 30, 2022 at 09:49 UTC

    You can avoid the maths with rindex:

    $ perl -le ' my @arr = (1,1,1,2,2,2,2,2,3,3,3,4,4,4,4,4,4,5,5,6); print rindex( join (q{}, @arr), 4 ); ' 16

    🦛