当前位置:网站首页>Perl入门学习(十)格式化输出
Perl入门学习(十)格式化输出
2022-07-28 05:24:00 【拾陆楼】
perl使用format定义输出模板,使用write按照模板输出。
1、format 格式
format需要定义格式名称,格式行,数据行与结束符号“.”,格式如下:
format FormatName = # 定义格式名称
fieldline # 格式行,用来定义一个输出行的格式,类似 @,^,<,>,| 这样的字符。
value_one, value_two, value_three # 数据行,用来向前面的格式行中插入值,都是perl的变量。
fieldline
value_one, value_two
.举例如下:
#!/usr/bin/perl
$text = "google it taobao";
format STDOUT =
first: ^<<<<< # 左边对齐,字符长度为6
$text
second: ^<<<<< # 左边对齐,字符长度为6
$text
third: ^<<<< # 左边对齐,字符长度为5,taobao 最后一个 o 被截断
$text
.
write运行结果如下:
$perl main.pl
first: google # 左边对齐,字符长度为6
second: it # 左边对齐,字符长度为6
third: taoba # 左边对齐,字符长度为5,taobao 最后一个 o 被截断2、格式行
格式行以@或者^开头,只定义格式不适用变量。
@ 字段(不要同数组符号 @ 相混淆)是普通的字段。
@,^ 后的 <, >,| 长度决定了字段的长度,如果变量超出定义的长度,那么它将被截断。
<, >,| 还分别表示,左对齐,右对齐,居中对齐。
^ 字段用于多行文本块填充。
举例如下:
#!/usr/bin/perl
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name, $age
@#####.##
$salary
===================================
.
select(STDOUT);
$~ = EMPLOYEE;
@n = ("Ali", "it", "Jaffer");
@a = (20,30, 40);
@s = (2000.00, 2500.00, 4000.000);
$i = 0;
foreach (@n){
$name = $_;
$age = $a[$i];
$salary = $s[$i++]; ## $i++意思是执行完之后$i再加1
write;
}运行结果如下:
$perl main.pl
===================================
Ali 20
2000.00
===================================
===================================
it 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================3、格式变量
$~ ($FORMAT_NAME) :格式名字
$^ ($FORMAT_TOP_NAME) :表头格式名字
$% ($FORMAT_PAGE_NUMBER) :页号
$= ($FORMAT_LINES_PER_PAGE):每页中的行数
$| ($FORMAT_AUTOFLUSH) :是否自动刷新输出缓冲区存储
举例如下:
#!/usr/bin/perl
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @##.##
$name, $age
@#####.## ## 格式行,采用整数5位,小数2位的输出格式
$salary
===================================
. ## 结束符.
# 添加分页 $%
format EMPLOYEE_TOP =
===================================
Name Age Page @< ## 格式行,左对齐
$% ## 数据行,输出页号
===================================
.
$~ = EMPLOYEE; ## 使用格式EMPLOYEE做报表
$^ = EMPLOYEE_TOP; ## 使用格式EMPLOYEE_TOP做表头
@n = ("Ali", "it", "Jaffer");
@a = (20,30, 40);
@s = (2000.00, 2500.00, 4000.000);
$i = 0;
foreach (@n){
$name = $_;
$age = $a[$i];
$salary = $s[$i++];
write;
}运行结果如下:
$perl main.pl
===================================
Name Age Page 1
===================================
===================================
Ali 20
2000.00
===================================
===================================
it 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================4、输出到其它文件
默认情况下函数write将结果输出到标准输出文件STDOUT,我们也可以使它将结果输出到任意其它的文件中。最简单的方法就是把文件变量作为参数传递给write,如:
write(MYFILE);使用select指定文件名,write后面就不需要选择文件名。
示例如下:
#!/usr/bin/perl
if (open(MYFILE, ">>tmp")) {
select (MYFILE); # 使得默认文件变量的打印输出到MYFILE中
$~ = "OTHER";
write; # 默认文件变量,打印到select指定的文件中,必使用$~指定的格式 OTHER
format OTHER =
=================================
使用定义的格式输入到文件中
=================================
.
close MYFILE;
}运行结果如下:
$ cat tmp
=================================
输入到文件中
=================================
=================================
使用定义的格式输入到文件中
=================================如果不使用select,那么format的格式名就需要与文件名相同。上面示例就要改成:
#!/usr/bin/perl
if (open(MYFILE, ">>tmp")) {
$~ = "OTHER"; ## 这里的$~没有作用,因为write的文件变量名MYFILE与
## format的格式名相同
write(MYFILE);
format MYFILE =
=================================
使用定义的格式输入到文件中
=================================
.
close MYFILE;
}边栏推荐
- vSphere ESXi 7.0 Update 3 发行说明
- frameset 用法示例
- ESXi 社区版网卡驱动
- Reading experience of protecting against DNN model steaming attacks
- (PHP graduation project) based on PHP Gansu tourism website management system to obtain
- 浅谈误码仪的使用场景?
- 将GrilView中的数据转换成DataTable
- ESXi社区版网卡驱动再次更新
- When to replace jack socket for dsx-pc6 jumper module?
- (PHP graduation project) obtained based on PHP novel website management system
猜你喜欢

在win7 上安装 Visual Studio 2019 步骤 及 vs2019离线安装包

Learning notes on hardware circuit design 2 -- step-down power circuit

Agilent Agilent e5071 test impedance and attenuation are normal, except crosstalk ng--- Repair plan

Random life-1

Transformer 自注意力机制 及完整代码实现

How to view the transfer function of the module directly built by Simulink

生活随机-1

3、 Openvino practice: image classification

Varistor design parameters and classic circuit recording hardware learning notes 5

Best practices to ensure successful deployment of Poe devices
随机推荐
EXFO 730c optical time domain reflectometer only has IOLm optical eye to upgrade OTDR (open OTDR permission)
Research on threat analysis and defense methods of deep learning data theft attack in data sandbox mode
Triode design, understanding saturation, linear region and cut-off region
vSphere ESXi 7.0 Update 3 发行说明
Summary of common WAF interception pages
CalendarExtender控件的中文显示问题
Fluke fluke aircheck WiFi tester cannot configure file--- Ultimate solution experience
8类网线测试仪AEM testpro CV100 和FLUKE DSX-8000哪些事?
开关电源电路EMI设计在layout过程中注意事项
arduino 读取模拟电压_MQ2气体/烟雾传感器如何工作及其与Arduino接口
Install visual studio 2019 steps and vs2019 offline installation package on win7
frameset 用法示例
TCL and eltcl? Cdnext and CMRL?
TCL和ELTCL?CDNEXT和CMRL?
WebService出错 Maximum message size quota for incoming messages (65536) has been exceeded.已超过传入消息(655
What is the AEM testpro cv100 and fluke dsx-8000 of category 8 network cable tester?
ESXi 社区版网卡驱动
(PHP graduation project) based on thinkphp5 community property management system
Internet of things interoperability system: classification, standards and future development
(PHP graduation design) obtained based on PHP fruit sales store management system