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 ...
随机推荐
- Java时间日期格式转换Date转String和String转Date
Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...
- 【转载】JSTL和EL的使用
使用JSTL前的准备 想要使用JSTL,首先需要给工程导入JSTL的包(JSTL.jar和standard.jar). JSTL标签库 在JSTL中分为以下五个标签 核心标签 格式化标签 SQL标签 ...
- eas之KDPrinter控件
初始化打印控件KDPrinter ctrlPrinter = new KDPrinter(); 增加列 // 指定插入位置table.addColumn(index);// 插入到最后table.ad ...
- 如何手动触发物理机panic,并产生vmcore
如何手动触发物理机panic,并产生vmcore? 1. 配置kdump 1.1 el6 如果是CentOS 6 则编辑/boot/grub/grub.conf配置在内核参数中添加 crashkern ...
- 字符串函数(day11)
使用存储区的地址作为返回值可以让调用 函数使用被调用函数的存储区 这种时候被调用函数需要提供一个指针类型 的存储区记录作为返回值的地址数据 不可以把非静态局部变量的地址作为返回值 使用 C语言里的文字 ...
- [bzoj4477 Jsoi2015]字符串树 (可持久化trie)
传送门 Solution 复习下tire( ̄▽ ̄)/ 裸的可持久化tire,我用树剖求了下LCA Code #include <cstdio> #include <cstring&g ...
- jQuery动态效果
1.一号店首页 2.淘宝网购物车
- CodeForces - 340 C - Tourist Problem
先上题目: A - Tourist Problem Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- snmptrap、snmpinform和snmptrapd的详细介绍及其用法
http://blog.csdn.net/reille/article/details/8712087
- 自建X509证书
X509证书是需要买的,自己建的证书不会被浏览器认可,会弹出提示安全不受保障的页面. 学习一下如何建,实际用到还是要买. 打开vs2015开发员人员命令提示 输入创建证书的命令 Makecert.ex ...