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

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

How can I calculate the array index for

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 11:57

    using List::Util, which is a core module, unlike List::MoreUtils, which is not:

    use List::Util qw(first);
    
    my @nums = ( '830974', '722065', '722046', '716963' );
    my $index = first { $nums[$_] eq '722065' } 0..$#nums;
    

提交回复
热议问题