什么是得到一个Perl数组的最后N个元素的最佳方式? [英] What's the best way to get the last N elements of a Perl array?

查看:106
本文介绍了什么是得到一个Perl数组的最后N个元素的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是获得一个Perl数组的最后N个元素的最佳方式?

如果数组有小于N,我不想在返回值一堆 undefs


解决方案

  @last_n = @source [ -  $ N ..- 1];

如果您不需要民主基金 s,则:

  @last_n =($ N> = @source)? @source:@source [ -  $ N ..- 1];

What's the best way to get the last N elements of a Perl array?

If the array has less than N, I don't want a bunch of undefs in the return value.

解决方案

@last_n = @source[-$n..-1];

If you require no undefs, then:

@last_n = ($n >= @source) ? @source : @source[-$n..-1];

这篇关于什么是得到一个Perl数组的最后N个元素的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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