Lists and Arrays
------------------
1. Lists and Arrays
1) A list is an ordered collection of scalars. An array is a variable that contains a list. In Perl, the two terms are often used as if they're interchangeable. But, to be accurate, the list is the data, and the array is the variable. You can have a list value that isn't in an array, but every array variable holds a list.
2) The subscript may be any expression that gives a numeric value. If it's not an integer already, it'll automatically be truncated to the next lower integer.
3) If the subscript indicates an element that would be beyond the end of the array, the corresponding value will be undef. This is just as with ordinary scalars; if you've never stored a value into the variable, it's undef.
2. Special Array Indics
1) If you store into an array element that is beyond the end of the array, the array is automatically extended as needed -- there's no limit on its length, as long as there's available memory for Perl to use. If intervening elements need to be created, they'll be created as undef values.
2) If you store into an array element that is beyond the end of the array, the array is automatically extended as needed -- there's no limit on its length, as long as there's available memory for Perl to use. If intervening elements need to be created, they'll be created as undef values.
3) The last element index of an array(e.g the variable name of the array is rocks) is $#rocks
4) Perl provide a shortcut: negative array indices count from the end of the array. If you've got three elements in the array, the valid negative indices are -1 (the last element), -2 (the middle element), and -3 (the first element).
3. List Literals
1) qw stands for "quoted words" or "quoted by whitespace," depending upon whom you ask. Either way, Perl treats it like a single-quoted string (so, you can't use \n or $fred inside a qw list as you would in a double-quoted string). The whitespace (characters like spaces, tabs, and newlines) will be discarded, and whatever is left becomes the list of items.
4. List Assignment
1) In much the same way as scalar values may be assigned to variables, list values may also be assigned to variables: ($fred, $barney, $dino) = ("flintstone", "rubble", undef);
2) Since the list is built up before the assignment starts, this makes it easy to swap two variables' values in Perl: ($fred, $barney) = ($barney, $fred);
3) In a list assignment, extra values are silently ignored -- Perl figures that if you wanted those values stored somewhere, you would have told it where to store them. Alternatively, if you have too many variables, the extras get the value undef.
4) Just use the at-sign (@) before the name of the array (and no index brackets after it) to refer to the entire array at once.
5) The value of an array variable that has not yet been assigned is ( ), the empty list. Just as new, empty scalars start out with undef, new, empty arrays start out with the empty list.
5. Interpolating Arrays into Strings
1) Like scalars, array values may be interpolated into a double-quoted string. Elements of an array are automatically separated by spaces upon interpolation.
2) A single element of an array will be replaced by its value, just as you'd expect:
@fred = qw(hello dolly);
$y = 2;
$x = "This is $fred[1]'s place"; # "This is dolly's place"
$x = "This is $fred[$y-1]'s place"; # same thing
Note that the index expression is evaluated as an ordinary expression, as if it were outside a string. It is not variable-interpolated first. In other words, if $y contains the string "2*4", we're still talking about element 1, not element 7, because "2*4" as a number (the value of $y used in a numeric expression) is just plain 2.
3) If you want to follow a simple scalar variable with a left square bracket, you need to delimit the square bracket so that it isn't considered part of an array reference, as follows:
@fred = qw(eating rocks is wrong);
$fred = "right"; # we are trying to say "this is right[3]"
print "this is $fred[3]\n"; # prints "wrong" using $fred[3]
print "this is ${fred}[3]\n"; # prints "right" (protected by braces)
print "this is $fred"."[3]\n"; # right again (different string)
print "this is $fred\[3]\n"; # right again (backslash hides it
6. The foreach Control Structure
1) The control variable is not a copy of the list element -- it actually is the list element. That is, if you modify the control variable inside the loop, you'll be modifying the element itself.
2) What is the value of the control variable after the loop has finished? It's the same as it was before the loop started. The value of the control variable of a foreach loop is automatically saved and restored by Perl. While the loop is running, there's no way to access or alter that saved value. So after the loop is done, the variable has the value it had before the loop, or undef if it hadn't had a value. That means that if you want to name your loop control variable "$rock", you don't have to worry that maybe you've already used that name for another variable, unless the variable name has been declared as a lexical in the current scope, in which case you get a lexically local variable instead of a package local.
7. Perl's Favorite Default: $_
1) If you omit the control variable from the beginning of the foreach loop, Perl uses its favorite default variable, $_. This is (mostly) just like any other scalar variable, except for its unusual name.
2) Remember that reverse operator returns the reversed list; it doesn't affect its arguments. If the return value isn't assigned anywhere, it's useless.
3) The sort operator sorts a list of values in the internal character ordering. And like what happened with reverse, the arguments themselves aren't affected. If you want to sort an array, you must store the result back into that array.
8. <STDIN> in List Context
1) <STDIN> returns the next line of input in a scalar context. Now, in list context, this operator returns all of the remaining lines up to the end of file. Each line is returned as a separate element of the list.
2)It turns out that if you give chomp a list of lines, it will remove the newlines from each item in the list.
分享到:
相关推荐
前端项目-angular-drag-and-drop-lists,Angular directives for sorting nested lists using the HTML5 Drag and Drop API
javascript js_leetcode题解之160-intersection-of-two-linked-lists.js
jquery-sortable-lists, 用于排序列表的jQuery插件还包括树结构 jquery-sortable-lists用于排序列表的插件还包括树结构。$('#myList').sortableLists( options );你可以通过鼠标对html列表的项进行排序。 创建树结构...
Lists-Lists-Lists
python python_leetcode题解之160_Intersection_of_Two_Linked_Lists.py
在Java编程中,数组、列表和结构化数据是核心概念,它们构成了程序处理和存储信息的基础框架。让我们深入探讨这些主题。 首先,数组是Java中最基本的数据结构之一,它允许我们存储固定数量相同类型的元素。...
"codecanyon-13300279-buzzy-news-viral-lists-polls-and-videos.zip" 是一个在Code Canyon上发布的项目文件包,主要用于创建一个新闻、病毒式列表、投票和视频分享的平台。这个平台可能具有高度互动性,让用户能够...
Learning Perl teaches you the basics and shows you how to write programs up to 128 lines long—roughly the size of 90% of the Perl programs in use today. Each chapter includes exercises to help you ...
js js_leetcode题解之23-merge-k-sorted-lists.js
js js_leetcode题解之21-merge-two-sorted-lists.js
c语言入门 C语言_leetcode题解之21-merge-two-sorted-lists.c
ngx拖放列表 ... 指令也可以嵌套以将拖放拖放到所见即所得编辑器,树或正在构建的任何奇特结构中。 归功于原始创作者 该库的灵感来自以AngularJS编写的库。 拖动元素输入 dndDraggable必需属性。 表示此元素是...
scribe-plugin-smart-lists 将文本项目符号转换为HTML列表 。 安装 bower install scribe-plugin-smart-lists 或者,您可以。 使用范例 scribe-plugin-smart-lists是一个AMD模块: require ( [ 'scribe' , 'scribe...
Chapter 4, Lists, Lists, and More Lists, covers the first data structures: lists. Using several examples, this chapter goes into variations of sequential data structures and their implementations. ...
资源分类:Python库 所属语言:Python 资源全名:django-smart-lists-1.0.14.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
**Lists and Arrays** - **章节概述**: 介绍Perl中的列表和数组数据结构及其操作方法。 - **学习目标**: 理解列表与数组的区别;掌握数组的操作技巧,包括排序、检索等。 - **关键知识点**: - 数组声明与初始化...
少插件列表 用于列表/数组操作的插件。...npm install -g less-plugin-lists 与lessc使用 lessc --lists file.less 有关通过命令行Less编译器使用插件的更多详细信息,请参见Less文档中的相应部分。