前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Perl Learning - 3 (A

Perl Learning - 3 (A

作者头像
py3study
发布于 2020-01-10 03:39:19
发布于 2020-01-10 03:39:19
1.1K0
举报
文章被收录于专栏:python3python3

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);

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Perl语言入门系列之一
Perl语言(https://www.perl.org/)最初是为文件体系处理而创作的一种多用途语言,Perl试图填补低级语言(如C、C++或汇编语言)和高级语言(如shell编程)之间的空白,使其既满足快速编程,又具有灵活的文本处理功能。Perl简单好用,但是比较难学,Perl为了提高运行速度,拥有大量简写、缩写,并拥有灵活的正则表达式系统,使得完成同一件任务可以有很多不同的方法。Perl脚本信息密度高,拥有很多浓缩语句(类似于普通语言里的“成语),因此可以用较短的代码完成更多的任务。一般Linux系统都会预安装perl,在Windows系统中运行Perl脚本则需要安装软件ActivePerl。
SYSU星空
2022/05/05
1.6K0
Perl语言入门系列之一
Perl语言入门系列之二
数值的比较操作符其含义与使用与数学中相同,而字符串的比较操作符则会对字符串从左到右进行一一对比,参照的是字符在ASCII编码或拓展到Unicode编码中的顺序(参照sort操作符),比较操作符会返回真(true)或假(false),如下所示:
SYSU星空
2022/05/05
1.3K0
Perl语言入门系列之二
perl语言十分钟入门【零基础可入】
零基础,perl语言,10分钟入门 1.Hello,World #!/usr/bin/perl -w print ("hello,world!\n"); #print "hello,world!\n"; 说明: (1)第一行指定解释器,-w参数表示提示警告(或者使用use strict命令,执行更严格的检查); (2)第二行输出hello, world!; (3)如果习惯c的函数方式,print的参数可以打括号; (4)第三行是注释,注释以#打头; (5)如果习惯shell的方式,print的参数可以没有括
架构师之路
2018/02/28
2.1K0
Perl_基本数据类型(2)
my @arr = split/:/,$str; # @arr = qw(A B C)
生信补给站
2020/08/06
5370
《Perl语言入门》——读书笔记
Perl语言入门 /** * prism.js Github theme based on GitHub's theme. * @author Sam Clarke */ code[class*="language-"], pre[class*="language-"] { color: #333; background: none; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.4; -moz-tab-size: 8; -o-tab-size: 8; tab-size: 8; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; } /* Code blocks */ pre[class*="language-"] { padding: .8em; overflow: auto; /* border: 1px solid #ddd; */ border-radius: 3px; /* background: #fff; */ background: #f5f5f5; } /* Inline code */ :not(pre) > code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; background: #f5f5f5; } .token.comment, .token.blockquote { color: #969896; } .token.cdata { color: #183691; } .token.doctype, .token.punctuation, .token.variable, .token.macro.property { color: #333; } .token.operator, .token.important, .token.keyword, .token.rule, .token.builtin { color: #a71d5d; } .token.string, .token.url, .token.regex, .token.attr-value { color: #183691; } .token.property, .token.number, .token.boolean, .token.entity, .token.atrule, .token.constant, .token.symbol, .token.command, .token.code { color: #0086b3; } .token.tag, .token.selector, .token.prolog { color: #63a35c; } .token.function, .token.namespace, .token.pseudo-element, .token.class, .token.class-name, .token.pseudo-class, .token.id, .token.url-reference .token.variable, .token.attr-name { color: #795da3; } .token.entity { cursor: help; } .token.title, .token.title .token.punctuation { font-weight: bold; color: #1d3e81; } .token.list { color: #ed6a43; } .token.inserted { background-color: #eaffea; color: #55a532; } .token.deleted { background-color: #ffecec; color: #bd2c00; } .token.bold { font-weight: bold; } .token.italic { font-style: italic; } /* JSON */ .lan
Homqyy
2023/03/06
2.5K0
Java面试专题之十:Mybatis相关的6个实用性问题
insert into names (name) values (#{value})
用户1289394
2020/11/23
3330
Java面试专题之十:Mybatis相关的6个实用性问题
【小家java】java11新特性(简述八大新特性) 首个重磅LTS版本
Java11 带来了 ZGC、Http Client 等重要特性,一共包含 17 个 JEP(JDK Enhancement Proposals,JDK 增强提案)。
YourBatman
2019/09/03
1.2K0
【小家java】java11新特性(简述八大新特性) 首个重磅LTS版本
秋招面试题系列- - -Java工程师(三)
11、Mybatis是如何将sql执行结果封装为目标对象并返回的?都有哪些映射形式?
叶秋学长
2022/08/17
4010
秋招面试题系列- - -Java工程师(三)
Perl 基础语法及数据结构
我也算学了基本脚本语言的人,Au3、Pyhon、PHP、JavaScript、Shell 都用过,但这个家伙是有点让我膛目,语法简直简练到不能再简练了。一下午的时间看了看他的基础语法和示例,做一下记录。
我与梦想有个约会
2023/10/21
1550
memcahced perl管理工具 memcached-tool
#  Andrey Niakhaichyk <andrey@niakhaichyk.org>
星哥玩云
2022/07/03
3080
2018-11-21
This section comprises a glossary of all the keywords — grouped by category and thence ordered lexicographically — in the Cypher query language.
Albert陈凯
2018/12/14
5290
Hive-1.2.1_04_DML操作 5.1. Join8.1. 使用案例8.2. Transform实现
当我们做Load操作是,hive不会做任何数据转换,只是纯复制/移动操作,将数据文件移动到与Hive表对应的位置。
踏歌行
2020/10/15
4230
Mybatis 的常见面试题「建议收藏」
4、通常一个Xml映射文件,都会写一个Dao接口与之对应,请问,这个Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗?
全栈程序员站长
2022/08/12
4930
部分计算机代数系统对比Computer Algebra Software: Mathematica, Maxima, Pari/GP
Computer Algebra Software: Mathematica, Maxima, Pari/GP
Enjoy233
2019/03/05
8070
PERL学习笔记---正则表达式的应用
//这是m//(模式匹配)的一种简写。同qw//操作一样,可以使用任何 成对的分隔符。因此,可以使用m(fred), m<fred>, m{fred}, m[fred],或者m,fred,, m!fred!, m^fred^,其它非成对的分隔符也 可以
yuanyuan
2019/09/10
7000
Vulnhub靶机:DC-9
靶机下载地址:https://download.vulnhub.com/dc/DC-9.zip
小简
2022/12/29
4920
Vulnhub靶机:DC-9
Perl语言入门系列之三:文件输入与输出
<>操作符俗称钻石操作符,它是行标准输入<STDIN>的特例,不是从键盘获得数据而是从用户指定的位置读取数据,这里的用户指定位置指运行程序时命令行程序后面紧跟的参数,也即命令行参数。
SYSU星空
2022/05/05
2.4K0
Perl语言入门系列之三:文件输入与输出
Perl语言程序应用(资料汇总版)
为了更加方便、快捷的实现某些功能,Perl拥有很多其他的控制结构,这些结构也许不是必需的,但可以使程序更加简洁和具有逻辑性。
SYSU星空
2022/05/05
3.6K0
Perl语言程序应用(资料汇总版)
Perl正则表达式(2) – 用正则表达式进行匹配
到目前为止,我们都是讲正则表达式的内容写在一对斜线内,如/fred/。但其实这是 m// 的简写,其中m代表match,和之前看到的 qw// 类似,我么可以自行选择用于保卫内容的一堆字符作为边界,所以上面这个例子可以改写为m{fred},m[fred],m!fred!等。
全栈程序员站长
2022/08/31
2.1K0
Perl正则表达式:文本处理
在正则表达式中s/正则表达式/替换字符/模式可以对特定字符串进行匹配查找并替换(substitution),如果匹配失败则不替换。其中s///可以理解为正则表达式的模式,也可以理解为操作符,其要修改的数据必须储存在变量,使用绑定操作符指定替换操作(不指定则使用默认变量$_),中如下所示:
SYSU星空
2022/05/05
4.9K0
Perl正则表达式:文本处理
相关推荐
Perl语言入门系列之一
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文