Perl 列表的上下文敏感度

Perl 列表的上下文敏感度

简介

在Perl中,函数调用、术语和语句的解释是不一致的,这取决于它的语境。在Perl中,有两个关键的上下文,即列表上下文和标量上下文。在列表上下文中,Perl给出的是元素的列表。但在标量上下文中,它返回数组中的元素数。
Perl在 “列表上下文 “中假定列表值,而列表可以有任何数量的元素,也可以只有一个元素,甚至可以是荒废的。

创建列表 上下文

列表上下文可以通过使用数组和列表来生成。

  • 对一个数组的赋值:

    示例:



@y = LIST;
@y = @z;
@y = localtime();

在这里,localtime()是Perl中的一个函数名称,它在数组中揭示了时间的数字描述。

  • 对一个列表的赋值:

示例:

(x,y) = LIST;
($x) =  LIST;

在这里,即使List只有一个元素,List也可以创建List Context。

例子

#!/usr/bin/perl
# Perl program of creating List Context
  
# array of elements
my @CS = ('geeks', 'for', 'geeks', 'articles'); 
              
# Assignment to a list 
my (x,y) = @CS; 
  
# Assignment to an Array
my @z = @CS;        
      
# Assignment of a function
# to an Array
my @t = localtime();
              
# Displays two elements of an
# array in List
print "xy\n";
  
# Displays an array of elements
print "@z\n";    
  
# Displays time stored in array 
# in number format
print @t;

输出。



geeks for
geeks for geeks articles
201761121191690

这里,在向List赋值时,List中有两个标量,即x和y,所以只赋值了数组的两个元素。

List Context中的数组

为了使用一个数组来激发List Context,我们需要将一个数组赋值给另一个数组。

例子



#!/usr/bin/perl
  
# Program for arrays in List Context
use strict;
use warnings;
use 5.010;
  
my @x = ('computer_', 'science_', 'portal_',
         'for_', 'GeeksforGeeks');
  
# Assignment of an array to 
# another array
my @y = @x; 
  
# Printing the new array
print @y;

输出。

computer_science_portal_for_GeeksforGeeks

这里,一个数组的元素被复制到另一个数组。

if-语句在列表中的使用

if-statement在List上下文中使用,只有当数组中存在元素时,才显示’if’所包含的语句。

例子

#!/usr/bin/perl
  
# Program to display content of if-statement
use strict;
use warnings;
use 5.010;
  
my @x = ('G', 'f', 'G');
   
# Statement within 'if' will be executed 
# only if the array is not empty
if (@x)
{
    print "GeeksforGeeks";
}

输出。

GeeksforGeeks

在这里,如果所述的数组有一些元素,那么if-condition为真,并引发if-statement的内容,但如果数组为空,那么if-condition为假,所以它不执行if-statement中的语句。

在列表背景下的读取。

在Perl中,”STDIN “是一个readline操作符。为了将readline操作符放在List Context中,需要将该操作符指定为一个数组。

例子

#!/usr/bin/perl
use strict;
use 5.010;
  
# Asking user to provide input 
print "Enter the list of names:\n";
  
# Getting input from user 
my @y = <STDIN>;
  
# Used to remove extra line of spaces
chomp @y;
  
# Printing the required output
print "The number of names are: " . 
                 scalar(@y) . "\n"; 

输出:

Perl  列表的上下文敏感度

以下是这个程序的工作原理:

第1步: 使用回车键逐一输入要存储在数组中的名字。

第2步: 在Linux系统中按下Ctrl-D,而在Windows系统中按下Ctrl-Z表示输入结束。

第3步: 使用chomp删除每次输入后增加的行。

第4步: 使用scalar打印数组中的元素数,因为 “scalar Context中的数组 “只能返回数组的长度。


Perl 教程
Perl 教程Perl 安装配置第一个 Perl 程序Perl 语法Perl 中的数据类型Perl 变量Perl 局部和全局变量Perl 中的标量Perl use strict和use warningsPerl 列表和数组Perl 中的哈希Perl 运算符Perl 中的条件语句Perl 中的if语句Perl 中的if-else语句perl 中的if-elsif-else语句Perl 中的unless语句Perl 中的unless-else语句Perl 中的 Switch CasePerl 中的 given-when-default 语句Perl 中的循环和循环控制语句Perl 中的 for 循环Perl while 循环Perl do-while 循环Perl foreach 循环Perl 中的until循环Perl 中的子程序Perl 字符串Perl 字符串转义序列Perl BarewordsPerl 比较文件的内容Perl 根据行号显示文件内容Perl 加密和解密Perl 错误处理Perl 格式Perl 函数签名Perl Hello World程序Perl 简介编写Perl代码的模式使用Perl的数字猜测游戏Perl 面向对象的编程(OOPs)Perl 包Perl abs()函数Perl 使用文件全局访问一个目录Perl Regex中的锚点Perl 向文件追加内容Perl 数组 pop() 函数Perl 数组切片Perl 数组 (push、pop、shift、unshift)Perl 数组Perl Regex中的断言Perl atan2() 函数Perl 自动加载(AUTOLOAD)功能Perl 字符串到数字的自动转换或铸造Perl 引用中的自生现象Perl 正则表达式中的回溯操作Perl Perl程序的基本语法Perl 布尔值Perl 调试器的中断点Perl CGI编程Perl chomp()函数Perl chop()函数Perl chr()函数Perl OOP中的类Perl cmp 操作符Perl 比较标量Perl 构造函数和析构函数Perl cos()函数Perl 计算文本中单词的频率Perl 创建Excel文件Perl 数据类型Perl 日期和时间Perl DBI(数据库独立接口)模块集Perl 分支 (if, if-else, Nested-if, if-elsif ladder, unless, unless-else, unless-elsif)Perl defined()函数Perl delete()函数Perl 带有CRUD操作的目录Perl 用调试器显示变量值Perl each() 函数Perl 正则表达式中的'e'修改器Perl 正则表达式中的'ee'修改器Perl OOPs中的封装Perl eq运算符Perl exists() 函数Perl 从脚本中退出Perl exp函数Perl 使用Regex从一个字符串中提取IP地址Perl 文件处理简介Perl 文件输入输出功能Perl 文件锁定Perl 文件测试运算符Perl CGI中的文件上传Perl 查找文件和目录Perl ge运算符Perl CGI中的GET与POSTPerl getc函数Perl 获取一个数组的元素数Perl given-when 语句Perl glob()函数Perl goto 语句Perl grep()函数Perl Regex中的分组和交替法Perl gt运算符Perl 标量和列表背景下的哈希值Perl 哈希操作Perl HashPerl 哈希值Perl hex函数Perl 实现一个队列Perl 实现一个堆栈Perl index() 函数Perl OOPs中的继承性Perl 在Windows、Linux和MacOS中的安装和环境设置Perl int()函数Perl join()函数Perl keys() 函数Perl 循环中的lastPerl lc()函数用于小写转换Perl lcfirst()函数Perl le运算符Perl length()函数Perl 调试器中的行操作命令Perl 列表及其类型Perl 列表的上下文敏感度Perl 列表函数Perl log() 函数Perl 循环 for, foreach, while, do...while, until, 嵌套循环Perl lt运算符Perl 匹配运算符
Perl 基础教程
Perl 简介Perl 环境安装Perl 基础语法Perl 数据类型Perl 变量Perl 标量Perl 数组Perl 哈希Perl 条件语句Perl 循环Perl 运算符Perl 时间日期Perl 子程序(函数)Perl 引用Perl 格式化输出Perl 文件操作Perl 目录操作Perl 错误处理Perl 特殊变量Perl 正则表达式Perl Socket 编程Perl 面向对象Perl 包和模块Perl 进程管理Perl POD 文档Perl while 循环Perl until 循环Perl for 循环Perl foreach 循环Perl do…while 循环Perl 循环嵌套Perl next 语句Perl last 语句Perl continue 语句Perl redo 语句Perl goto 语句Perl IF 语句Perl IF…ELSE 语句Perl IF…ELSIF 语句Perl UNLESS 语句Perl UNLESS…ELSE 语句Perl UNLESS…ELSIF 语句Perl switch 语句
Perl 问答
Perl 和 Ruby 的区别