home PYTHONJAVA
 

Perl formatted output

Perl is a very powerful text data processing language.

Perl can use format to define a template and then use write to output data in the specified template.

Perl formatting definition syntax is as follows:

format FormatName =
Fieldline
Value_one, value_two, value_three
Fieldline
Value_one, value_two
.

Parameter resolution:

  • FormatName : Format the name.
  • fieldline : A format line that defines the format of an output line, like @,^,<,>,|.
  • value_one, value_two... : The data line, used to insert values into the previous format line, are perl variables.
  • . : End symbol.

The following is a simple example of formatting:

Instance

#!/usr/bin/perl $text= " google welookups google"; format STDOUT = first: ^<<<<<< # Aligned to the left, the character length is 6 $text second: ^<<<<<< # Aligned to the left, the character length is 6 $text third: ^<<<< # Align left, character length is 5, google last o is truncated $text . write

Execute the above example output as:

first: google
Second: welookups
Third: google

Format line (graphic line) syntax

  • Format lines start with @ or ^, and these lines are not substituted for any form of variables.
  • The @ field (not to be confused with the array symbol @) is a normal field.
  • The length of
  • @,^ after the <, >,| determines the length of the field, and if the variable exceeds the defined length, it will be truncated.
  • <, >,| Also means left aligned, right aligned, centered aligned.
  • The ^ field is used for multi-line text block filling.

Value field format

The format of the range is as shown in the following table:

Format Value Meaning
@<<< Left aligned output
@>>> right aligned output
@||| Medium Aligned Output
@##.##   Fixed precision numbers  
@* Multiple lines of text

The first character of each range is a line filler, and when using the @ character, no text formatting.

In the above table, except for the multi-line value range @*, the field width is equal to the number of characters specified by the character @, for example:

@###.##

means seven characters wide, four decimal places, and two decimal places.

Examples are as follows:

Instance

#!/usr/bin/perl format EMPLOYEE = ============================================ @<<<<<<<<<<<<<<<<<<<<<<<<<<<> $name, $age @#####.## $salary ============================================ . select(STDOUT); $~ = EMPLOYEE; @n = ( "Ali", "welookups ", "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 output of the above example is:

============================== ======
Ali 20
  2000.00
================================================= >
================================================= >
welookups 30
  2500.00
================================================= >
================================================= >
Jaffer 40
  4000.00
================================================= >

Format Variables

  • $~ ($FORMAT_NAME) : format name $^ ($FORMAT_TOP_NAME) : The current header format name is stored in
  • $% ($FORMAT_PAGE_NUMBER) : The page number of the current output
  • $= ($FORMAT_LINES_PER_PAGE) : Number of rows per page
  • $| ($FORMAT_AUTOFLUSH) : Whether to automatically refresh the output buffer storage
  • $^L ($FORMAT_FORMFEED) : The string to be output before each page (except the first page) header is stored in

The following is an example of a simple format using $~:

Instance

#!/usr/bin/perl $~ = "MYFORMAT"; # Specify the format used under the default file variable write; # Output $~ specified Format format MYFORMAT = # Define the format MYFORMAT ================================== Text # welookups Tutorial ================================== . write;

Execute the above example output as:

============================== ====
      Text # welookups Tutorial
============================================== Span class="pln">
============================================== Span class="pln">
      Text # welookups Tutorial
============================================== /pre>

If you don't specify $~, a format called STDOUT will be output:

Instance

#!/usr/bin/perl write; # don't specify $~ In case you will find a format called STDOUT format STDOUT = ~ The text specified with the ~ sign will not be output. ---------------- STDOUTformat ---------------- .

Execute the above example output as:

----------------
  STDOUTformat
----------------

In the following example, we demonstrate the use of the $^ or $FORMAT_TOP_NAME variable by adding a report header:

Instance

#!/usr/bin/perl format EMPLOYEE = ============================================ @<<<<<<<<<<<<<<<<<<<<<<<<<<<> $name, $age @#####.## $salary ============================================ . format EMPLOYEE_TOP = ============================================ Name Age ============================================ . select(STDOUT); $~ = EMPLOYEE; $^ = EMPLOYEE_TOP ; @n = ( "Ali", "welookups ", "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 output of the above example is:

============================== ======
Name Age
================================================= >
================================================= >
Ali 20
  2000.00
================================================= >
================================================= >
welookups 30
  2500.00
================================================= >
================================================= >
Jaffer 40
  4000.00
================================================= >

We can also set up pagination for reports using $% or $FORMAT_PAGE_NUMBER:

Instance

#!/usr/bin/perl format EMPLOYEE = ============================================ @<<<<<<<<<<<<<<<<<<<<<<<<<<<> $name, $age @#####.## $salary ============================================ . # Add a page $% format EMPLOYEE_TOP = ============================================ Name Age Page @< $% ============================================ . select(STDOUT); $~ = EMPLOYEE; $^ = EMPLOYEE_TOP ; @n = ( "Ali", "welookups ", "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 output of the above example is:

============================== ======
Name Age Page 1
================================================= >
================================================= >
Ali 20
  2000.00
================================================= >
================================================= >
welookups 30
  2500.00
================================================= >
================================================= >
Jaffer 40
  4000.00
================================================= >

Output to other files

By default, the function write outputs the result to the standard output file STDOUT. We can also output the result to any other file. The easiest way is to pass the file variable as a parameter to write, such as:

write(MYFILE);

The above code write is output to the file MYFILE with the default print format named MYFILE.

But this way you can't use the $~ variable to change the print format used. The system variable $~ works only with the default file variable. We can change the default file variable, change $~, and then call write.

Instance

#!/usr/bin/perl if (open(MYFILE, ">tmp ")) { $~ = "MYFORMAT"; write MYFILE; # Contains the output of the file variable, which will print the same name as the variable, MYFILE. The value specified in $~ is ignored. format MYFILE = # with the same name as the file variable ================================== Input to file ================================== . close MYFILE; }

After the execution is successful, we can view the contents of the tmp file as follows:

$ cat tmp
============================================== Span class="pln">
      Enter in file
============================================== /pre>

When we can use select to change the default file variable, it returns the internal representation of the current default file variable, so that we can create subroutines that output as we want without affecting the rest of the program.

Instance

#!/usr/bin/perl if (open(MYFILE, ">>tmp")) { select (MYFILE); # Make the default file variable printout to MYFILE $~ = "OTHER"; write; # default file variable, print To the file specified by select, the format specified by $~ must be used OTHER format OTHER = ================================== Enter into the file using the defined format ================================== . close MYFILE; }

After the execution is successful, we can view the contents of the tmp file as follows:

$ cat tmp
============================================== Span class="pln">
      Enter in file
============================================== Span class="pln">
============================================== Span class="pln">
  Enter into the file using the defined format
============================================== /pre>








welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.