Logo

Programming-Idioms

History of Idiom 222 > diff from v27 to v28

Edit summary for version 28 by skeletonkey:
New Perl implementation by user [skeletonkey]

Version 27

2021-08-16, 02:36:12

Version 28

2021-09-28, 14:40:54

Idiom #222 Find first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Idiom #222 Find first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Variables
i,x,items
Variables
i,x,items
Extra Keywords
position
Extra Keywords
position
Code
$i = -1;
$index = 0;
foreach (@items) {
    $i = $index, last if $x eq $items[$index];
    $index++;
}
Demo URL
http://codepad.org/FHaOiLuX