在Perl中,如何在数组中找到给定值的索引?

时间:2022-11-01 21:31:39
$VAR1 = [
          '830974',
          '722065',
          '722046',
          '716963'
        ];

How can I calculate the array index for the value "722065"?

如何计算值“722065”的数组索引?

8 个解决方案

#1


33  

The firstidx function from List::MoreUtils can help:

List :: MoreUtils的第一个firstidx函数可以帮助:

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

#2


24  

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

使用List :: Util,这是一个核心模块,与List :: MoreUtils不同,它不是:

use List::Util qw(first);

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

#3


14  

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.

如果您只需要第一个索引,则应使用List :: MoreUtils :: first_index。

#4


7  

If you only need to look up the one item, use firstidx as others have said.

如果你只需要查找一个项目,请使用firstidx,正如其他人所说的那样。

If you need to do many lookups, build an index.

如果需要进行多次查找,请构建索引。

If your array items are unique, building an index is quite simple. But it's not much more difficult to build one that handles duplicate items. Examples of both follow:

如果您的数组项是唯一的,那么构建索引非常简单。但是构建一个处理重复项目的方法并不困难。两者的例子如下:

use strict;
use warnings;

use Data::Dumper;

# Index an array with unique elements.
my @var_uniq  = qw( 830974 722065 722046 716963 );
my %index_uniq  = map { $var_uniq[$_] => $_ } 0..$#var_uniq;

# You could use hash slice assinment instead of map:
# my %index_uniq;
# @index_uniq{ @var_uniq } = 0..$#var_uniq

my $uniq_index_of_722065   = $index_uniq{722065};
print "Uniq 72665 at: $uniq_index_of_722065\n";
print Dumper \%index_uniq;

# Index an array with repeated elements.
my @var_dupes = qw( 830974 722065 830974 830974 722046 716963 722065 );
my %index_dupes;
for( 0..$#var_dupes ) {
    my $item = $var_dupes[$_];

    # have item in index?
    if( $index_dupes{$item} ) {
        # Add to array of indexes
        push @{$index_dupes{$item}}, $_;
    }
    else {
        # Add array ref with index to hash.
        $index_dupes{$item} = [$_];
    }
}

# Dereference array ref for assignment:
my @dupe_indexes_of_722065 = @{ $index_dupes{722065} };

print "Dupes 722065 at: @dupe_indexes_of_722065\n";
print Dumper \%index_dupes;

#5


3  

Here's hastily written attempt at a reverse look-up using a hash.

这是使用哈希进行反向查找的匆匆写入尝试。

my $VAR1 = [ '830974', '722065', '722046', '716963' ];

my %reverse;
$reverse{$VAR1->[$_]} = $_ for 0 .. @$VAR1 - 1;

print $reverse{722065};

This does not account for arrays with duplicate values. I do not endorse this solution for production code.

这不会考虑具有重复值的数组。我不认可这个生产代码的解决方案。

#6


1  

check out the Perl FAQ

查看Perl常见问题解答

#7


0  

use strict;
use Data::Dumper;

sub invert
{
 my $i=0;
 map { $i++ => $_ } @_;
}

my @a = ('a','b','c','d','e');

print Dumper @a;

print Dumper invert @a;

#8


-7  

it's okay, everyone was new to perl at one point

没关系,每个人都有新的perl

$a is the element to print the index of in @list...

$ a是打印@list中索引的元素...

my @list = (124124, 323, 156666, 124412, 3333, 4444444444, 124124124, 24412);
my $a = 4444444444;

print 
substr($_=($,=
chr($==39)).(
join$,,@list).$,,$=-$=,
index$_,$,.$a.$,)=~
tr/'//+$=---$=;

#1


33  

The firstidx function from List::MoreUtils can help:

List :: MoreUtils的第一个firstidx函数可以帮助:

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

#2


24  

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

使用List :: Util,这是一个核心模块,与List :: MoreUtils不同,它不是:

use List::Util qw(first);

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

#3


14  

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.

如果您只需要第一个索引,则应使用List :: MoreUtils :: first_index。

#4


7  

If you only need to look up the one item, use firstidx as others have said.

如果你只需要查找一个项目,请使用firstidx,正如其他人所说的那样。

If you need to do many lookups, build an index.

如果需要进行多次查找,请构建索引。

If your array items are unique, building an index is quite simple. But it's not much more difficult to build one that handles duplicate items. Examples of both follow:

如果您的数组项是唯一的,那么构建索引非常简单。但是构建一个处理重复项目的方法并不困难。两者的例子如下:

use strict;
use warnings;

use Data::Dumper;

# Index an array with unique elements.
my @var_uniq  = qw( 830974 722065 722046 716963 );
my %index_uniq  = map { $var_uniq[$_] => $_ } 0..$#var_uniq;

# You could use hash slice assinment instead of map:
# my %index_uniq;
# @index_uniq{ @var_uniq } = 0..$#var_uniq

my $uniq_index_of_722065   = $index_uniq{722065};
print "Uniq 72665 at: $uniq_index_of_722065\n";
print Dumper \%index_uniq;

# Index an array with repeated elements.
my @var_dupes = qw( 830974 722065 830974 830974 722046 716963 722065 );
my %index_dupes;
for( 0..$#var_dupes ) {
    my $item = $var_dupes[$_];

    # have item in index?
    if( $index_dupes{$item} ) {
        # Add to array of indexes
        push @{$index_dupes{$item}}, $_;
    }
    else {
        # Add array ref with index to hash.
        $index_dupes{$item} = [$_];
    }
}

# Dereference array ref for assignment:
my @dupe_indexes_of_722065 = @{ $index_dupes{722065} };

print "Dupes 722065 at: @dupe_indexes_of_722065\n";
print Dumper \%index_dupes;

#5


3  

Here's hastily written attempt at a reverse look-up using a hash.

这是使用哈希进行反向查找的匆匆写入尝试。

my $VAR1 = [ '830974', '722065', '722046', '716963' ];

my %reverse;
$reverse{$VAR1->[$_]} = $_ for 0 .. @$VAR1 - 1;

print $reverse{722065};

This does not account for arrays with duplicate values. I do not endorse this solution for production code.

这不会考虑具有重复值的数组。我不认可这个生产代码的解决方案。

#6


1  

check out the Perl FAQ

查看Perl常见问题解答

#7


0  

use strict;
use Data::Dumper;

sub invert
{
 my $i=0;
 map { $i++ => $_ } @_;
}

my @a = ('a','b','c','d','e');

print Dumper @a;

print Dumper invert @a;

#8


-7  

it's okay, everyone was new to perl at one point

没关系,每个人都有新的perl

$a is the element to print the index of in @list...

$ a是打印@list中索引的元素...

my @list = (124124, 323, 156666, 124412, 3333, 4444444444, 124124124, 24412);
my $a = 4444444444;

print 
substr($_=($,=
chr($==39)).(
join$,,@list).$,,$=-$=,
index$_,$,.$a.$,)=~
tr/'//+$=---$=;