数组和在Perl的负面指标 [英] Arrays and negative indexes in Perl

查看:99
本文介绍了数组和在Perl的负面指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的Perl 新手,我读有关数组。结果
据我了解在需要时自动阵列扩展(酷!)结果
但我也看了,我们可以使用负的索引来访问以相反的顺序数组。结果
例如。 3元素数组可以访问为:搜索
$数组[0]数组$ [1] $数组[2] 结果
或结果
$阵列[-1] $阵列[-2] $阵列[-3] (以相反的顺序)。结果
我的问题是不是 -3 较小的值,例如发生了什么 $阵列[-5] ?结果
请问阵列扩展还是怎么了?

I am newbie in Perl and I am reading about arrays.
As I understand the arrays expand automatically as needed (cool!)
But I also read that we can use negative indexes to access the arrays in reverse order.
E.g. an array of 3 elements can be accessed as:
$array[0] $array[1] $array[2]
or
$array[-1] $array[-2] $array[-3] (in reverse order).
My question is what happens for values smaller than -3 e.g. $array[-5]?
Does the array expand or something?

推荐答案

如果你读它,结果是一样的读 $阵列[5] - 价值不存在,你会得到一个民主基金了。要关闭端的左侧和结束会向右边是相同的。

If you read it, the result is the same as reading $array[5] — the value doesn't exist and you get an undef out. Going off the end to the left and going off the end to the right are the same.

如果你写的,你会得到一个错误。数组只能自动扩展的权利。

If you write it, you get an error. Arrays can only auto-extend to the right.

这篇关于数组和在Perl的负面指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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