设为首页 收藏本站
查看: 950|回复: 0

[经验分享] Note of Learning Perl--Lists and Arrays

[复制链接]

尚未签到

发表于 2017-5-18 12:51:19 | 显示全部楼层 |阅读模式
  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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-378815-1-1.html 上篇帖子: Note of Learning Perl--Scalar Data 下篇帖子: perl动态创建zip压缩文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表