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的更多相关文章

  1. Nginx - HTTP Configuration, Module Directives

    Socket and Host Configuration This set of directives will allow you to configure your virtual hosts. ...

  2. 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 ...

  3. [转载]两个半小时学会Perl

    Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...

  4. 在vi中使用perltidy格式化perl代码

    格式优美的perl代码不但让人赏心悦目,并且能够方便阅读. perltidy的是sourceforge的一个小项目,在我们写完乱七八糟的代码后,他能像变魔术一样把代码整理得漂美丽亮,快来体验一下吧!! ...

  5. Perl技巧

    项目里面一直用的是Perl,Perl里有各种小技巧就分享在这吧. push(@a, $b) 把b元素压入a数组中, 还可以有 push(@a, [@b]); 那a就成了二维数组了 scalar(@a) ...

  6. PCRE Perl Compatible Regular Expressions Learning

    catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...

  7. perl脚本基础总结

    1.  单引号字符串中的\n不会被当做换行符处理. 如:'\'\\'  -->  '\  . 2.  双引号 字符串联    "Hello"."World" ...

  8. 34 Sources for Test Ideas

    We recommend collecting test ideas continuously from a variety of information sources. Consider the ...

  9. Perl参考函数

    这是标准的Perl解释器所支持的所有重要函数/功能的列表.在一个函数中找到它的详细信息. abs - 绝对值函数 accept - 接受传入的socket连接 alarm - 调度一个SIGALRM ...

随机推荐

  1. php 加密解密函数封装

    算法一: //加密函数 function lock_url($txt,$key='yang') { $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi ...

  2. spring IOC 装配一个bean

    1.0属性注入 新建一个people类 package com.java.test3; /** * @author nidegui * @create 2019-06-22 14:45 */ publ ...

  3. Java基础学习笔记三 正则表达式和校验、Date、DateFormat、Calendar

    正则表达式 正则表达式(英语:Regular Expression,在代码中常简写为regex).正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串.在 ...

  4. python tkinter模块小工具界面

    代码 #-*-coding:utf-8-*- import os from tkinter import * root=Tk() root.title('小工具') #清空文本框内容 def clea ...

  5. 【Leetcode】【简单】【169求众数】【JavaScript】

    题目 169. 求众数 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [ ...

  6. [luogu2329 SCOI2005] 栅栏(二分+搜索)

    传送门 Solution 纯搜索80分,加二分90分,再补一个小剪枝满分qwq 真.小剪枝:如果下一个的需求和当前相同,那么不需要再次从头开始试(看代码就明白了233) Code #include & ...

  7. [luogu4290 HAOI2008]玩具取名(DP)

    传送门 Solution 裸区间DP Code #include <map> #include <cmath> #include <cstdio> #include ...

  8. Problem 52

    Problem 52 It can be seen that the number, 125874, and its double, 251748, contain exactly the same ...

  9. 生成PW配置和BFD配置

    保存在配置文件中 def Main(): buf = '' f = open('pw.cfg','w') for i in range(2016): pwid = str(102 + i) buf + ...

  10. 用DIME格式来组织自定义格式

    直接网际消息封装(Direct Internet Message Encapsulation,即DIME)格式提供了一种简单而又标准的机制,这个机制可以把多文本(multiple text)和二进制数 ...