当前位置:网站首页>Perl basic grammar
Perl basic grammar
2022-08-09 04:50:00 【acmgotoac】
标量数据
perlThe scalar in language is mostlydigital scalar和字符串标量.
- 数字
- 浮点数
3.25e20 (3.25乘10的20次方) - 整数
1415926 1_415_926 (perlUnderscores are allowed between integers for clear identification) - 数字运算符
| 运算符 | 意义 |
|---|---|
| ±*/ | 加减乘除 |
| % | 取模 |
| ** | 乘方 |
注意:perlInteger and floating point numbers in are as perdoublestorage calculation.
- 字符串
- 单引号
where string is the string itself,不会转义. - 双引号
\会转义 - Period operator.
Strings before and after concatenation - 重复操作符x
copy字符串,如果copyThe number of times is a decimal,则向下取整 - 数字与字符串之间的转换
perlThe data conversion is done by itself according to the operator
- 标量变量
- 命名规则
以$开头;
Variable identifiers consist of alphanumeric underscores,但不能以数字开头;
变量区分大小写; - 等号赋值
- Binary assignment operator
+= -= *= /= %= - String variable interpolation
- Numeric scalar and character scalar comparison operators
| 数值标量 | Character scalar |
|---|---|
| == | eq |
| != | ne |
| < | lt |
| > | gt |
| <= | le |
| >= | ge |
- chomp函数
Remove newlines at the end of a string - chop函数
Remove characters from the end of the string - substr函数
Take out some characters,Returns the extracted string
substr EXPR,OFFSET,LENGTH,REPLACEMENT
#EXPR String handle
#OFFSET 处理起始位置,默认为0,Negative numbers are counted from the right
#LENGTH 处理长度,负数时,从右侧数LENGTH个点,Take the characters between this point and the starting point,From start to end when not provided
#REPLACEMENT 替换字符串
- split Split the string into an array
split /pattern/, EXPR, LIMIT
#if only pattern is given, EXPR defaluts to $_
#pattern 分隔符,Regular when possible,也可以指定,defaluts为" ",省略分隔符,Consecutive whitespace is considered a single delimiter
#LIMIT 限制数组长度,Default maximum number of splits
- join Inserts the given character into an array,转换为字符串
数组和哈希
- 数组
#!/usr/bin/perl
use strict;
use warnings;
my $scalar = "wow";
#数组定义
my @array = (1, "str", $scalar);
#元素调用
print "$array[0]\n";
#元素连接
my $array_str = join "\t", @array;
print "$array_str\n"
#数组长度 方法一
my $len = $#array + 1;
print "$len\n";
#数组长度 方法二
my $len2 = scalar @array; #scalar可以省略
print "$len2\n";
#遍历数组 方法一
foreach (@array)
{
print "$_\n" #$_ Indicates the default place,foreach No readout element specified,就存在$_中
}
#遍历数组 方法二
foreach (0..$#array)
{
print "$array[$_]\n";
}
#数组末尾添加元素 push
push @array, "push";
print "@array\n";
#Remove elements from the end of the array pop
pop @array;
print "@array\n";
#Add elements to the beginning of the array unshift
unshift @array, "unshift";
print "@array\n";
#数组开头删除元素 shift
shift @array;
print "@array\n";
#颠倒数组元素顺序 reverse 不会改变原数组
my @array_reverse = reverse @array;
print "reverse answer : @array_reverse \n";
#数组排序 sort 不会改变原数组
my @array_sort1 = sort @array; #按照ASCIICode default order,从小到大排序
print "ASCII sort lu: @array_sort1\n";
my @array_sort2 = sort {
$b cmp $a} @array; #按照ASCII从大到小排序
print "ASCII sort ul: @array_sort2\n";
my @array_sort3 = sort {
$b <=> $a} (4, 1, 9, 10, 100); #Follow the numbers from largest to smallest
print "num sort ul: @array_sort3\n";
#可以自定义排序规则 sort sub_fun @array;
- 哈希
重要规则:1.keys唯一 2.Key-value pairs are arranged out of order in the hash
#!/usr/bin/perl
use strict;
use warnings;
#hash的定义 Both single and double quotes can be used when defining
my %hash = ("red", "1", 'blue', "2", "yellow", "3");
#my %hash = ("red"=> "1", "blue"=> "2", "yellow"=> "3");
#调用hash元素
print "hash element ; $hash{'red'}\n"; #You can only use single quotes, not double quotes
#Add or change key pairs
$hash{
'red'} = "new1";
print "hash new element : $hash{'red'}\n";
#返回hash的所有keys或者所有values
my @keys = keys %hash;
print "keys of hash : @keys\n";
my @values = values %hash;
print "values of hash : @values\n";
#遍历hash键对
while (my ($k,$v) = each %hash)
{
print "keys-values : $k-$v\n";
}
foreach my $keys (sort {
$hash{
$b} cmp $hash{
$a}} keys %hash)#ASCII 从大到小的keys排序
{
print "keys-values ASCII ul : $keys-$hash{$keys}\n";
}
#判断hash中是否有某个key
print "yes\n" if exists $hash{
'red'};
流程控制结构
- boolValues and logical operators
- bool
| 数据类型 | 规定 |
|---|---|
| digital scalar | 0为假,其他为真 |
| 字符串标量 | ‘’,"0"为假,其他为真 |
| undef | 假 |
- 逻辑运算符
&& || !
条件判断
if…elsif…else
unless(条件为假时进入)else…
三目运算符… : …?循环
while for foreach each循环控制
| 类型 | 规定 |
|---|---|
| last | 退出当前循环,Do not exit the outer loop,类似break |
| next | 跳过本次循环,进入下次循环,类似continue |
| redo | Ignore what follows,Repeat this cycle |
IOand file reading and writing
- IO操作
print 不自带\n
say 自带\n
printf 格式化输出字符串
sprintf 格式化,不输出
| 格式符 | 含义 |
|---|---|
| %% | % |
| %s | 字符串 |
| %d | 整型数字 |
| %f | 浮点型数字 |
| %e | 科学计算法 |
#!/usr/bin/perl
use strict;
use warnings;
printf "%010.2f\n", 3.1415126;
#%010.2f
#0 Sets the character width for padding characters
#10 设置字符宽度为10
#.2 设置显示2位小数
#f 输出浮点型
#输出字符串,字符宽度为10,向右对齐,宽度不足用0补齐,By default padding with spaces
printf "%010s\n", "haha";
- 读写文件操作
open函数
open file, "<", $filestring;
while(<>)
边栏推荐
- php将在线远程文件写入临时文件
- 【Harmony OS】【ArkUI】ets开发 创建视图与构建布局
- 全栈代码测试覆盖率及用例发现系统的建设和实践
- 学习笔记_numpy图片基本操作_自用
- OKR management process, how to implement effective dialogue, using the CFR feedback and recognition?
- AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'
- 【ITRA】2022年ITRA赛事注册流程 从0-1
- 【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
- Base64编码和图片转化
- P1163 银行贷款
猜你喜欢

存储系统架构演变

Quantitative Genetics Heritability Calculation 2: Half Siblings and Full Siblings

How to do the stability test, this article thoroughly explains it!

杰理之采用mix out eq 没有作用【篇】

2022 Security Officer-B Certificate Exam Practice Questions and Online Mock Exam

稳定性测试怎么做,这篇文章彻底讲透了!

JS-DOM-对象的事件onload、匿名函数、this

Alibaba Cloud Tianchi Contest Question (Machine Learning) - Prediction of Industrial Steam Volume (Complete Code)

equals and ==

MySQL: redo log log - notes for personal use
随机推荐
[21天学习挑战赛——内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
数量遗传学遗传力计算2:半同胞和全同胞
I.MX6U-ALPHA开发板(串口实验)
【Harmony OS】【ARK UI】公共事件模块
【Harmony OS】【ARK UI】轻量级数据存储
存储系统架构演变
【Harmony OS】【ARK UI】自定义弹窗
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
Introduction to JVM garbage collection mechanism
Quantitative Genetics Heritability Calculation 2: Half Siblings and Full Siblings
equals and ==
抖音直播新号怎么起号?抖音直播间不进人怎么办?
换座位[异或巧妙的让奇偶互换]
【Harmony OS】【ArkUI】ets开发 创建视图与构建布局
Disappearance of heritability - wiki
MySQL: Intent Shared Locks and Intentional Exclusive Locks | Deadlocks | Lock Optimization
【HMS Core】【FAQ】【AR Engine】AR Engine FAQ
【Harmony OS】【FAQ】Hongmeng Questions Collection 1
【HMS core】【ML kit】机器学习服务常见问题FAQ
遗传力缺失的案例