📄 ch19.htm
字号:
long, you use <TT><FONT FACE="Courier">@<<<<<<<<<<</FONT></TT>
as the format specifier. Listing 19.4 centers the text in the
middle of the page.
<HR>
<BLOCKQUOTE>
<B>Listing 19.4. Right- and left-justifying code.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 while(<>) {<BR>
3 ($chemical, $found) = split(':');
<BR>
4 write;<BR>
5 }<BR>
6 format STDOUT_TOP =<BR>
7 Chemical
Product<BR>
8 ===================================================<BR>
9 .<BR>
10 format STDOUT =<BR>
11 @>>>>>>>>>>>>>>>>>>>>
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<BR>
12 $chemical, $found<BR>
13 .</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here is the right- and left-justified text output.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">
Chemical Product<BR>
===================================================<BR>
Acetic
Acid Vinegar<BR>
Ammonium Hydroxide Ammonia Cleaners
<BR>
Ammonium Nitrate Salt Peter
<BR>
Ammonium Oleate Ammonia
Soap<BR>
Barium Sulfide Black
Ash<BR>
Carbon Carbinate Chalk<BR>
Carbontetrachloride Cleaning Fluid<BR>
Calcium Oxide Lime
<BR>
Ferric Oxide Rust
<BR>
Glucose Corn
Syrup<BR>
Graphite Pencil Lead<BR>
Hydrogen Peroxide Peroxide
<BR>
Naphthalene Mothballs
<BR>
Silver Nitrate Photographer's
Hypo<BR>
Sodium Bicarbonate Baking Soda<BR>
Sodium Borate Borax
<BR>
Sodium Carbonate Washing Liquids
<BR>
Sodium Chloride Salt
<BR>
Sodium Silicate Glass
<BR>
Sulfuric Acid Battery
Acid<BR>
Sucrose Cane
Sugar</FONT></TT>
</BLOCKQUOTE>
<P>
Look at line 11 in Listing 19.4 and compare it with line 11 in
Listing 19.3. Then, compare the output of each of those programs.
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Caution</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
You can create the best-looking reports in your Perl script and have them present beautifully on your xterm. However, unless you ensure that your terminal and printing device both use fixed-width fonts, lining up the text columns will be a nightmare. When
in doubt, use fonts like Courier (on printers) or Fixed (on X Window System terminals). Avoid fonts like Helvetica or New Century Schoolbook because these are variable-width fonts, and you'll never really be able to align the characters and columns as you
would be able to with fixed-width fonts.</BLOCKQUOTE>
<BLOCKQUOTE>
This type of inconsistency in outputs is fairly obvious when you try to print your Perl- formatted reports on Web browsers that are set on variable-width fonts. Do not expect a Web browser to be set on fixed-width fonts. If you must print such reports,
consider using an HTML page table instead. See Part IV, "Working with the Web," for more information.</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Numbers in the format field are specified with the hash mark (<TT><FONT FACE="Courier">#</FONT></TT>).
Let's try a different example with a new data file containing
both numeric and text data. The idea is to print out the values
in this data file in a nice report.
<P>
Here is the sample data file with text and numeric fields.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">UK , 44 , Pound , 1.85 , 100<BR>
BELGIUM , 32 , Franc , 32.0 , 200<BR>
DENMARK , 45 , Krone , 6.0 , 2000<BR>
FINLAND , 358 , Markka , 4.69, 1000<BR>
FRAncE , 33 , Franc , 5.28, 50<BR>
ELSALVADOR , 503 , Colon , 8.74, 340<BR>
PHILIPPINES , 63 , Peso , 24.8, 1000<BR>
PAKISTAN , 92 , Rupee , 38.0, 1200<BR>
BAHRAIN , 973 , Dinar , 0.38 , 45<BR>
IRAQ , 964 , Dinar , 0.60, 10<BR>
JORDAN , 962 , Dinar , 0.70, 100<BR>
SAUDIARABIA , 966 , Riyal , 3.75, 1000</FONT></TT>
</BLOCKQUOTE>
<P>
This file contains the names of countries, their international
dialing codes, their currencies, and the value of the currencies
with respect to the dollar. (Keep in mind that I made up these
numbers.) The fifth value is the number of currency bills on hand.
<P>
The value of the fourth item in each row is where numbers have
different numbers of digits to the right of the decimal point.
This output is a fragment from a spreadsheet comma-delimited file
that did print unequal numbers of decimal digits.
<P>
Listing 19.5 takes this file as input and generates a nice, clean
report. (I am deliberately not using the amount field in this
program.)
<HR>
<BLOCKQUOTE>
<B>Listing 19.5. Printing numeric and text fields.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 $count = 0;<BR>
3 while(<>) {<BR>
4 ($country, $code, $currency, $value)
= split(',');<BR>
5 $count++;<BR>
6 write;<BR>
7 }<BR>
8 format STDOUT_TOP =<BR>
9 Id Country
Code Currency Rate<BR>
10 =============================================<BR>
11 .<BR>
12 format STDOUT =<BR>
13 @## @<<<<<<<<<<<<<<<<<@####@<<<<<<<<@#####.##
<BR>
14 $count, $country, $code, $currency, $value<BR>
15 .</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Notice how in line 13 the <TT><FONT FACE="Courier">$counter</FONT></TT>
variable is printed in two digits using the <TT><FONT FACE="Courier">@##</FONT></TT>
format. The name of the country is left-justified with the <TT><FONT FACE="Courier">@<<</FONT></TT>
symbol, followed immediately by the area code number. If the country's
name is too long to fit in the specified area, it is truncated
to fit with no spaces between the country's name and code.
<P>
The <TT><FONT FACE="Courier">$value</FONT></TT> is shown with
two digits to the right of the decimal point. Even though the
input did not have the same number of digits to the right of the
decimal point, the output will be formatted with two digits to
the right of the decimal point. In fact, the output will be cleanly
aligned on the decimal point, as shown here:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Id Country
Code Currency Rate<BR>
=============================================<BR>
1 UK
44 Pound 1.85<BR>
2 BELGIUM 32
Franc 32.00<BR>
3 DENMARK 45
Krone 6.00<BR>
4 FINLAND
358 Markka 4.69<BR>
5 FRAncE 33
Franc 5.28<BR>
6 ELSALVADOR 503
Colon 8.74<BR>
7 PHILIPPINES 63
Peso 24.80<BR>
8 PAKISTAN
92 Rupee 38.00<BR>
9 BAHRAIN
973 Dinar 0.38
<BR>
10 IRAQ 964
Dinar 0.60<BR>
11 JORDAN 962
Dinar 0.70<BR>
12 SAUDIARABIA 966
Riyal 3.75</FONT></TT>
</BLOCKQUOTE>
<P>
You are not limited to printing only variables in the formatted
statement. Because the variables in the format specification are
evaluated by Perl, you can place statements in there as well.
Consider the program in Listing 19.6, which prints the result
of a calculation.
<HR>
<BLOCKQUOTE>
<B>Listing 19.6. Calculations in the format statement.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 $count = 0;<BR>
3 while(<>) {<BR>
4 ($country, $code, $currency, $value,
$amount)<BR>
5 =
split(',');<BR>
6 $count++;<BR>
7 write;<BR>
8 }<BR>
9 format STDOUT_TOP =<BR>
10 Id Country Currency Rate Amount Value
in $<BR>
11 =====================================================<BR>
12 .<BR>
13 format STDOUT =<BR>
14 @## @<<<<<<<<<<<< @<<<<<<
@#####.## @####.## $ @######.##<BR>
15 $count, $country, $currency, $value, $amount, ($amount/$value)
<BR>
16 .</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here's the output of the run on the data file.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Id Country Currency Rate Amount
Value in $<BR>
==============================================================
<BR>
1 UK Pound
1.85 100.00 $ 54.05
<BR>
2 BELGIUM Franc 32.00
200.00 $ 6.25<BR>
3 DENMARK Krone
6.00 2000.00 $
333.33<BR>
4 FINLAND Markka 4.69 1000.00 $
213.22<BR>
5 FRAncE Franc
5.28 50.00 $
9.47<BR>
6 ELSALVADOR Colon
8.74 340.00 $ 38.90
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -