Several Ideas on Perl List Context
According to Beginning Perl Book published by Tsinghua Pub., the list context appears when you are trying to assign some value to a list variable.
my @copy = @source;
This is a very simple instance of shallow copy, which usually means you just copied the reference of the source array, none new elements are created and no other memories will be occupied.
When we assign an array with a hash table, the key-value elements will be converted to a plain list, which is called planarization.
For example, the following perl codes will print the result below:
my %hashtable = (
Tom=>,
Jerry=>,
Sam=>
);
my @flattened = %hashtable;
print @flattened;
Tom12Jerry13Sam17
*Another thing I want to stress here is, if we want to seperate these flattened elements by a blank space charactor, we need to modify the print statement to this: (Attention to the quatation marks)
print "@flattened";
Thus, the result in console will become:
Tom 12 Jerry 13 Sam 17
The book has noted one important feature - the advantage of list context:
You can force to obtain it with a pair of brackets.
If we want to assign the first element to a scalar variable, we can simply surround it with brackets, like this:
my @array = ('element0','element1');
my ($scalar) = @array;
print $scalar;
The result is displayed:
element0
But there is more than that! We can add more scalars to the brackets(as long as the array has enough elements), and the scalars will be assigned by the elements one by one. Code it as follow:
my @array = ('element0','element1');
my ($scalar0,$scalar1);
print "$scalar0\t$scalar1";
The result is:
element0 element1
Have you found the advantage of list context? If you haven't, see the followint statement:
($leftScalar , $rightScalar) = ($rightScalar , $leftScalar);
Isn't it beautiful? Usually, especially in some other languages, we have to use a third variable to store one of the two elements and that certainly takes more than this in Perl.
OK! It's what we get this afternoon!
Several Ideas on Perl List Context的更多相关文章
- Nginx - HTTP Configuration, Module Directives
Socket and Host Configuration This set of directives will allow you to configure your virtual hosts. ...
- Pattern Recognition and Machine Learning-02-1.0-Introduction
Introduction The problem of searching for patterns in data is a fundamental one and has a long and s ...
- [转载]两个半小时学会Perl
Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...
- 在vi中使用perltidy格式化perl代码
格式优美的perl代码不但让人赏心悦目,并且能够方便阅读. perltidy的是sourceforge的一个小项目,在我们写完乱七八糟的代码后,他能像变魔术一样把代码整理得漂美丽亮,快来体验一下吧!! ...
- Perl技巧
项目里面一直用的是Perl,Perl里有各种小技巧就分享在这吧. push(@a, $b) 把b元素压入a数组中, 还可以有 push(@a, [@b]); 那a就成了二维数组了 scalar(@a) ...
- PCRE Perl Compatible Regular Expressions Learning
catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...
- perl脚本基础总结
1. 单引号字符串中的\n不会被当做换行符处理. 如:'\'\\' --> '\ . 2. 双引号 字符串联 "Hello"."World" ...
- 34 Sources for Test Ideas
We recommend collecting test ideas continuously from a variety of information sources. Consider the ...
- Perl参考函数
这是标准的Perl解释器所支持的所有重要函数/功能的列表.在一个函数中找到它的详细信息. abs - 绝对值函数 accept - 接受传入的socket连接 alarm - 调度一个SIGALRM ...
随机推荐
- 范畴论-一个单子(Monad)说白了不过就是自函子范畴上的一个幺半群而已
范畴即为结构:包含要素和转化. 范畴为高阶类型. 函子为高阶函数.函子的输入为态射.函子为建立在态射基础上的高阶函数.函子用于保持范畴间映射的结构.态射用于范畴内部的转换. 群为运算规则的约束. 自函 ...
- console.log、toString方法与js判断变量类型
Java调用system.print.out()是会调用toString方法打印js里的console.log也是控制台打印,很多时候,我们以为也是调用toString方法,其实并不是.我们在chro ...
- Postgresql_最新版11.2源码编译安装
pg官网:https://www.postgresql.org/ yum -y gcc gcc-c++ cmake ncurses-devel perl zlib* .去官网下载源码包. 下载地址:h ...
- Python字符串格式化--formate()的应用
1.简单运用字符串类型格式化采用format()方法,基本使用格式是:转自 <模板字符串>.format(<逗号分隔的参数>) 调用format()方法后会返回一个新的字符串, ...
- BZOJ2212——线段树合并
学习线段树合并,以这道题为契机 多谢这篇博客 这里是通过对线段树合并时,顺手统计了对于一颗子树内,是否反转两种情况的逆序对数 这里只对代码进行详细分析,见注解好了 #include<cstdio ...
- dd命令测试IO
在实际环境中,测试IO写性能 首先需要实时监测磁盘的IO sar -d interval count 同时对磁盘进行IO压力写 time dd if=/dev/zero of=baa.img bs=1 ...
- 【JavaScript游戏开发】使用HTML5 canvas开发的网页版中国象棋项目
//V1.0 : 实现棋子的布局,画布及游戏场景的初始化 //V2.0 : 实现棋子的颜色改变 //V3.0 :实现所有象棋的走棋规则 //V4.0 : 实现所有棋子的吃子功能 完整的项目源码已经开源 ...
- iOS_网络请求_代理方式
#pragma mark - 网络请求代理方式(异步) - (IBAction)DelegateButtonDidClicked:(UIButton *)sender { // 1.拼接 urlStr ...
- 继续过Hard题目
接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧. # Title Editorial Acceptance ...
- Thread.yield()方法表示交出主动权,join表示等待当前线程,可以指定秒数
Thread.yield()方法表示交出主动权,join表示等待当前线程,可以指定秒数 学习了:http://www.importnew.com/14958.html 膜拜一下 源码膜拜: Threa ...