Learning Perl: 3.1. Accessing Elements of an Array
来源:互联网 发布:淘宝直邮美国 编辑:程序博客网 时间:2024/06/15 18:47
3.1. Accessing Elements of an Array
If you've used arrays in another language, you won't be surprised to find Perl provides a way to subscript an array to refer to an element by a numeric index.
The array elements are numbered using sequential integers, beginning at zero and increasing by one for each element, like this:
$fred[0] = "yabba"; $fred[1] = "dabba"; $fred[2] = "doo";
The array name (in this case, "fred") is from a completely separate namespace than scalars use. You could have a scalar variable named $fred in the same program. Perl treats them as different things and doesn't get confused.[*] (Your maintenance programmer might be confused though, so don't capriciously make all of your variable names the same.)
[*] The syntax is always unambiguous; tricky perhaps, but unambiguous.
You can use an array element like $fred[2] in every place[] where you could use any other scalar variable like $fred. For example, you can get the value from an array element or change that value by the same sorts of expressions we used in the previous chapter:
[] Well, almost. The most notable exception is that the control variable of a foreach loop, which you'll see later in this chapter, must be a simple scalar. And there are others, like the "indirect object slot" and "indirect filehandle slot" for print and printf.
print $fred[0]; $fred[2] = "diddley"; $fred[1] .= "whatsis";
Of course, the subscript may be any expression that gives a numeric value. If it's not an integer, it'll automatically be truncated to the next lower integer:
$number = 2.71828; print $fred[$number - 1]; # Same as printing $fred[1]
If the subscript indicates an element that would be beyond the end of the array, the corresponding value will be undef. This is the same as ordinary scalars; if you've never stored a value into the variable, it's undef.
$blank = $fred[ 142_857 ]; # unused array element gives undef $blanc = $mel; # unused scalar $mel also gives undef
- Learning Perl: 3.1. Accessing Elements of an Array
- Find all max of elements of an array
- Find all max of elements of an array
- get an array of arrays with unique elements
- JSP for loop output all elements of an array
- Given an array of integers, sort the array according to frequency of elements
- Find the two non-repeating elements in an array of repeating elements
- The minimum number of elements to reach the end of an array
- Matching Portions of A String to Elements of an Array in Ruby
- Accessing Members of an Enclosing Class
- 【AWK】Scanning all Elements of an Array--每日一译2013-02-05
- How can I combine elements of an array that have a common element?
- Perl Learning 2 List&Array
- Find consecutive elements in an array
- Three Increasing Elements in an Array
- Remove duplicate elements in an array
- Size of an array
- LeetCode_697_Degree of an Array
- Learning Perl: 2.12. Exercises
- LINUX上安装memcached
- Java异常框架中finally执行、及其他相关问题
- Struts2开发知识点
- Ubuntu下交叉编译tslib-1.4记录
- Learning Perl: 3.1. Accessing Elements of an Array
- hdu 3460 Ancient Printer
- 智能卡3G COS软件的设计与实现
- Learning Perl: 3.2. Special Array Indices
- domino使用URL进行数据搜索
- java的package机制
- Learning Perl: 3.3. List Literals
- Cannot create JDBC driver of class 'com.mysql.jdbc
- Learning Perl: 3.4. List Assignment