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

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

How can I calculate the array index for

7条回答
  •  [愿得一人]
    2020-12-01 12:01

    Here is how you would find all the positions at which a given value appears:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my @x = ( 1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1 );
    my @i = grep { $x[$_] == 3 } 0 .. $#x;
    print "@i\n";
    

    If you only need the first index, you should use List::MoreUtils::first_index.

提交回复
热议问题