在Perl中,我怎么能找到一个给定值的数组索引? [英] In Perl, how can I find the index of a given value in an array?

查看:95
本文介绍了在Perl中,我怎么能找到一个给定值的数组索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我如何计算值722065数组索引?

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

推荐答案

从的 firstidx 函数=htt​​p://search.cpan.org/perldoc /列表%3A%3AMoreUtils>列表:: MoreUtils 可以帮助:

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

这篇关于在Perl中,我怎么能找到一个给定值的数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆