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

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

问题描述

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

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

解决方案

firstidx 函数rel="noreferrer">List::MoreUtils 可以提供帮助:

使用严格;使用警告;使用 List::MoreUtils qw(firstidx);我的@nums = ('830974', '722065', '722046', '716963');printf "列表中索引为 %i 的项目是 722065\n", firstidx { $_ eq '722065' } @nums;__结尾__

<前>列表中索引为 1 的项目是 722065

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

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

解决方案

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天全站免登陆