In Perl, how can I find the index of a given value in an array?

前端 未结 7 1791
迷失自我
迷失自我 2020-12-01 11:15
$VAR1 = [
          \'830974\',
          \'722065\',
          \'722046\',
          \'716963\'
        ];

How can I calculate the array index for

7条回答
  •  失恋的感觉
    2020-12-01 12:04

    The firstidx function from List::MoreUtils can help:

    use strict;
    use warnings;
    use List::MoreUtils qw(firstidx);
    
    my @nums = ( '830974', '722065', '722046', '716963' );
    printf "item with index %i in list is 722065\n", firstidx { $_ eq '722065' } @nums;
    
    __END__
    
    item with index 1 in list is 722065
    

提交回复
热议问题