当前位置:网站首页>Perl Introduction (10) formatted output
Perl Introduction (10) formatted output
2022-07-28 06:30:00 【Shilu building】
perl Use format Define output templates , Use write Output by template .
1、format Format
format You need to define the format name , Format line , Data line and end symbol “.”, The format is as follows :
format FormatName = # Define format name
fieldline # Format line , Used to define the format of an output line , similar @,^,<,>,| Such characters .
value_one, value_two, value_three # data row , Used to insert values into the previous format line , All are perl The variable of .
fieldline
value_one, value_two
.Examples are as follows :
#!/usr/bin/perl
$text = "google it taobao";
format STDOUT =
first: ^<<<<< # Align left , The character length is 6
$text
second: ^<<<<< # Align left , The character length is 6
$text
third: ^<<<< # Align left , The character length is 5,taobao the last one o Truncated
$text
.
writeThe operation results are as follows :
$perl main.pl
first: google # Align left , The character length is 6
second: it # Align left , The character length is 6
third: taoba # Align left , The character length is 5,taobao the last one o Truncated 2、 Format line
Format lines are in @ perhaps ^ start , Define only the format, not the variables .
@ Field ( Don't use array symbols @ To confuse ) It's a normal field .
@,^ After <, >,| The length determines the length of the field , If the variable exceeds the defined length , Then it will be truncated .
<, >,| It also means , Align left , Right alignment , Align center .
^ Fields are used for multi line text block filling .
Examples are as follows :
#!/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++ It means after execution $i add 1
write;
}The operation results are as follows :
$perl main.pl
===================================
Ali 20
2000.00
===================================
===================================
it 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================3、 Format variable
$~ ($FORMAT_NAME) : Format name
$^ ($FORMAT_TOP_NAME) : Header format name
$% ($FORMAT_PAGE_NUMBER) : Page number
$= ($FORMAT_LINES_PER_PAGE): Number of lines per page
$| ($FORMAT_AUTOFLUSH) : Whether to automatically refresh the output buffer storage
Examples are as follows :
#!/usr/bin/perl
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @##.##
$name, $age
@#####.## ## Format line , Use integer 5 position , decimal 2 Bit output format
$salary
===================================
. ## Terminator .
# Add pagination $%
format EMPLOYEE_TOP =
===================================
Name Age Page @< ## Format line , Align left
$% ## data row , Output page number
===================================
.
$~ = EMPLOYEE; ## Use format EMPLOYEE Make reports
$^ = EMPLOYEE_TOP; ## Use format EMPLOYEE_TOP Make the header
@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;
}The operation results are as follows :
$perl main.pl
===================================
Name Age Page 1
===================================
===================================
Ali 20
2000.00
===================================
===================================
it 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================4、 Output to other files
By default, functions write Output the results to the standard output file STDOUT, We can also make it output the results to any other file . The simplest way is to pass file variables as parameters to write, Such as :
write(MYFILE);Use select Specify a filename ,write You don't need to choose a file name later .
Examples are as follows :
#!/usr/bin/perl
if (open(MYFILE, ">>tmp")) {
select (MYFILE); # Print out the default file variables to MYFILE in
$~ = "OTHER";
write; # Default file variables , print to select In the specified file , Must use $~ Specified format OTHER
format OTHER =
=================================
Use the defined format to input into the file
=================================
.
close MYFILE;
}The operation results are as follows :
$ cat tmp
=================================
Import into file
=================================
=================================
Use the defined format to input into the file
=================================If not used select, that format The format name of the file needs to be the same as the file name . The above example should be changed to :
#!/usr/bin/perl
if (open(MYFILE, ">>tmp")) {
$~ = "OTHER"; ## there $~ It doesn't work , because write File variable name of MYFILE And
## format The format name of is the same
write(MYFILE);
format MYFILE =
=================================
Use the defined format to input into the file
=================================
.
close MYFILE;
}边栏推荐
- Surge impact immunity experiment (surge) -emc series Hardware Design Notes 6
- 雷达成像 Matlab 仿真 2 —— 脉冲压缩与加窗
- 测量电脑电池容量
- mixup_ ratio
- Fluke dtx-1800 and its accessories dtx-cha002 channel adapter channel replacement RJ45 socket notes
- 雷达成像 Matlab 仿真 3 —— 多目标检测
- 低功耗设计-Power Switch
- EMC experiment practical case ESD electrostatic experiment
- EfficientNET_V1
- MySQL join skills
猜你喜欢

机器学习笔记 5 —— Logistic Regression

mixup_ratio

set_ case_ analysis

VAN(DWConv+DWDilationConv+PWConv)

ASP. Net read database bound to treeview recursive mode

Low power design -power switch

Graduation thesis | how to write literature review

Selection of PLC

Word自动目录字体修改和行间距的问题

Create a basic report using MS chart controls
随机推荐
I heard that you are also practicing when I interviewed several junior interns.
Matlab simulation of radar imaging 3 - multi-target detection
Trouble encountered in cable testing -- a case study of a manufacturer?
ICC2(四)Routing and Postroute Optimization
ICC2分析时序的神器 analyze_design_violations
CString to char[] function
Exploration of Clickhouse aggregation internal mechanism of aggregation
Fluke dtx-1800 and its accessories dtx-cha002 channel adapter channel replacement RJ45 socket notes
天线效应解决办法
低功耗设计-Power Switch
USB network native driver for esxi updated to support esxi7.0.1
Vs code basic configuration and beautification
Low power design -power switch
Briefly introduce EMD decomposition, Hilbert transform and spectral method
Install visual studio 2019 steps and vs2019 offline installation package on win7
Cronbach’s α? Kmo coefficient? Factor load? The most understandable course of questionnaire reliability and validity analysis in history!!! (SPSS and AMOS)
Why should fluke dsx2-5000 network cable tester be calibrated once a year?
Surge impact immunity experiment (surge) -emc series Hardware Design Notes 6
mixup_ ratio
Beta distribution (probability of probability)