返回顶部
首页 > 资讯 > 后端开发 > Python >Perl Learning - 3 (A
  • 727
分享到

Perl Learning - 3 (A

PerlLearning 2023-01-31 02:01:03 727人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

List & Array   While Scalar is single value, list is a list of scalars in order. Every element of a list is a depend

List & Array
 
While Scalar is single value, list is a list of scalars in order. Every element of a list is a dependant scalar, it can be number or characters.
Array is the variable of list, list is the values of array. In Perl array and list are almost the same meaning: a list of scarlars.
Every element of array is kept with its index, starting from [0].
 
$fred[0]="yabba";
$fred[1]="dabba";
$fred[2]="doo";
 
Arrays and Scalars have different namespaces, such as $fred[0] and $fred can be used at the same time, Perl won't be confused, but maybe the maintainer will, so don't play Perl like that.
We can do whatever to array elements like we do to a scalar.
 
print $fred[0];
$fred[2]="didley";
print $fred[$number-1];
 
Like the last example, we can use variable and expression in array's index. Element without a value will be undef.
Array can grow its length automatically if you give values to a certain index.
 
$rocks[0]='bedrock';
$rocks[1]='slate';
$rocks[2]='lava';
$rocks[3]='crushed rock';
$rocks[99]='schist';      # 95 undef elements created!
 
There are two ways to directly get the last element of an array:
 
$rocks[$#rocks]='hard rock';
$rocks[-1]='hard rock';
$#rocks is the index of last element, both above ways are correct, but [-1] is more popular ^_^
 
list/array can be writen in ( ), split by ',' between neighbour elements.
 
(1, 2, 3)
(1, 2, 3,) # same as above
()  # empty list, containing 0 element
(1 .. 100) # a list of 100 int
(5 .. 1) # empty list, .. is order sensitive
(2, 2 .. 6, 10, 12 ) # same as (0, 2, 3, 4, 5, 6, 10, 12)
("fred", "barney", "betty", "milma", "dino") # list of characters
($m .. $n)
(0 .. $#rocks)
($m, 17) # two values
($m+$0, $p+$q) # two values
 
The last 4 examples shows element can be variable and/or expression too.
 
List of characters are very common, qw was designed for this. qw means 'quotes Words', it works as a piar of ' .
 
qw(fred barney betty wilma dino) # same as ('fred', 'barney', 'betty', 'milma', 'dino')
qw will ignore the whitespace (spaces, tabs newlines), and the ( ) can be other symbols.
qw ! fred barney betty wilma dino !
qw# fred barney betty wilma dino # # like comments
qw( fred barney betty wilma dino )
qw{ fred barney betty wilma dino }
qw[ fred barney betty wilma dino ]
qw< fred barney betty wilma dino >
 
qw{
/usr/dict/words
/home/rootbeer/.ispell_english # a Good way representing Unix paths
}
 
Like scalar, we can give values to list.
 
($fred, $barney, $dino)=("flintstone", "rubble", undef);
($fred,$barney)=($barney,$fred); # a simple way to replace values of varables
($betty[0],$betty[1])=($betty[1],$betty[0]);
($fred,$barney) = qw<flintstone rubble slate granite>; # slate and granite will be ignored
($wilma,$dino) = qw[flintstone];   # $dino is undef
 
We can use @ to give values to an array.
 
@rocks=qw/bedrock slate lava/;
@tiny=();   # empty list
@stuff=(@giant, undef, @giant);
 

--结束END--

本文标题: Perl Learning - 3 (A

本文链接: https://lsjlt.com/news/185495.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • Perl Learning - 3 (A
    List & Array   While scalar is single value, list is a list of scalars in order. Every element of a list is a depend...
    99+
    2023-01-31
    Perl Learning
  • perl学习笔记(3)
    条件结构: if(...){       ...; }elsif(...){       ...; }else{       ...; } 数值关系运算符 ==,>...
    99+
    2023-01-31
    学习笔记 perl
  • Perl开发的学习-3
    计算机作为一种计算工具,其优势在于执行重复任务和根据条件进行大量判断(因此循环语句,条件语句及顺序执行语句是计算机控制结构中基本语句,而与人类语言中句法完全不同)。 [root@windriver-machine t...
    99+
    2023-01-31
    Perl
  • int **a[3][4] 和 size
    BS的《C++编程》里面讲得很清楚,变量的申明,变量名称的后面部分比前面部分具有更强的约束力。所以,如下变量声明:int **a[3][4];表示“int **”类型的二维数组(“[]”比“int”和“*”更具有约束力)。这个二维数组共有1...
    99+
    2023-01-31
    int size
  • 附件3:eclipse memory a
    http://jingyan.baidu.com/article/ce09321b620a3d2bff858ff5.html简单使用:分析三步曲:通常我们都会采用下面的“三步曲”来分析内存泄露问题:首先,对问题发生时刻的系统内存状态获取一个...
    99+
    2023-01-31
    附件 eclipse memory
  • javascript中如何让“a==1&&a==2&&a==3”成立
    这篇文章主要讲解了“javascript中如何让“a==1&&a==2&&a==3”成立”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“javascript中如...
    99+
    2023-07-05
  • 面试题:怎么让“a==1&&a==2&&a==3”成立?
    问题是可以解决了。评论区的有掘友说让 a = true 也可以解决的。确实很有误导性。其实是混淆了隐式转化的优先级。简单来说,隐式转化是由两部分组成:转化的规则 + 触发转化的条件。if包着的里面整体触发了Boolean()的转化规则,==...
    99+
    2023-05-14
    面试题 javascript
  • 聊聊Python中关于a=[[]]*3的反思
    Python 关于a=[[]]*3的反思 之前用python做了一个关于交通大数据的项目,由于之前比较赶进度,故现在会陆续更新对项目代码的一些反思。 由此可以看出,a[0],a[1],a[2]指向的是同一个元素...
    99+
    2022-06-02
    Python a=[[]]*3
  • c语言中a的3次方怎么表示
    c 语言中表示 a 的 3 次方有两种方法:使用 pow() 函数计算幂次。使用直接乘法运算符 a a a。 C 语言中表示 a 的 3 次方的两种方法 C 语言中表示 a 的 3...
    99+
    2024-05-02
    c语言
  • Django:The value of 'list_display[3]' refers to 'account_admin', which is not a call
    初学Django,Django 修改models.py 字段后执行makemigrations报错,报错信息:<class 'test2.admin.AccountAdmin'>:&n...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作