⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch19.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
long, you use <TT><FONT FACE="Courier">@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</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">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 while(&lt;&gt;) {<BR>

&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp; ($chemical, $found) = split(':');

<BR>

&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp; write;<BR>

&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp; }<BR>

&nbsp;6 format STDOUT_TOP =<BR>

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Chemical

Product<BR>

&nbsp;8 ===================================================<BR>

&nbsp;9 .<BR>

10 format STDOUT =<BR>

11 @&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;

@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;

<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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Chemical Product<BR>

===================================================<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Acetic

Acid&nbsp;&nbsp;Vinegar<BR>

&nbsp;&nbsp;&nbsp;Ammonium Hydroxide&nbsp;&nbsp;Ammonia Cleaners

<BR>

&nbsp;&nbsp;&nbsp;&nbsp; Ammonium Nitrate&nbsp;&nbsp;Salt Peter

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ammonium Oleate&nbsp;&nbsp;Ammonia

Soap<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Barium Sulfide&nbsp;&nbsp;Black

Ash<BR>

&nbsp;&nbsp;&nbsp;&nbsp; Carbon Carbinate&nbsp;&nbsp;Chalk<BR>

&nbsp;&nbsp;Carbontetrachloride&nbsp;&nbsp;Cleaning Fluid<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Calcium Oxide&nbsp;&nbsp;Lime

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ferric Oxide&nbsp;&nbsp;Rust

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Glucose&nbsp;&nbsp;Corn

Syrup<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Graphite&nbsp;&nbsp;Pencil Lead<BR>

&nbsp;&nbsp;&nbsp;&nbsp;Hydrogen Peroxide&nbsp;&nbsp;Peroxide

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Naphthalene&nbsp;&nbsp;Mothballs

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Silver Nitrate&nbsp;&nbsp;Photographer's

Hypo<BR>

&nbsp;&nbsp;&nbsp;Sodium Bicarbonate&nbsp;&nbsp;Baking Soda<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sodium Borate&nbsp;&nbsp;Borax

<BR>

&nbsp;&nbsp;&nbsp;&nbsp; Sodium Carbonate&nbsp;&nbsp;Washing Liquids

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sodium Chloride&nbsp;&nbsp;Salt

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sodium Silicate&nbsp;&nbsp;Glass

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sulfuric Acid&nbsp;&nbsp;Battery

Acid<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sucrose&nbsp;&nbsp;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, &quot;Working with the Web,&quot; 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">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 $count = 0;<BR>

&nbsp;3 while(&lt;&gt;) {<BR>

&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp; ($country, $code, $currency, $value)

= split(',');<BR>

&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp; $count++;<BR>

&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp; write;<BR>

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp; }<BR>

&nbsp;8 format STDOUT_TOP =<BR>

&nbsp;9 Id&nbsp;&nbsp;Country&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Code&nbsp;&nbsp;Currency&nbsp;&nbsp;&nbsp;&nbsp;Rate<BR>

10 =============================================<BR>

11 .<BR>

12 format STDOUT =<BR>

13 @## @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;@####@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;@#####.##

<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">@&lt;&lt;</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&nbsp;&nbsp;Country&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Code&nbsp;&nbsp;Currency&nbsp;&nbsp;&nbsp;&nbsp;Rate<BR>

=============================================<BR>

&nbsp;&nbsp;1 UK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

92 Rupee&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 38.00<BR>

&nbsp;&nbsp;9 BAHRAIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

973 Dinar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.38

<BR>

&nbsp;10 IRAQ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;964

Dinar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.60<BR>

&nbsp;11 JORDAN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;962

Dinar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.70<BR>

&nbsp;12 SAUDIARABIA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;966

Riyal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 $count = 0;<BR>

&nbsp;3 while(&lt;&gt;) {<BR>

&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp; ($country, $code, $currency, $value,

$amount)<BR>

&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=

split(',');<BR>

&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp; $count++;<BR>

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp; write;<BR>

&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp; }<BR>

&nbsp;9 format STDOUT_TOP =<BR>

10 Id&nbsp;&nbsp;Country&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Currency&nbsp;&nbsp;Rate&nbsp;&nbsp;&nbsp;Amount&nbsp;&nbsp;Value

in $<BR>

11 =====================================================<BR>

12 .<BR>

13 format STDOUT =<BR>

14 @## @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&nbsp;&nbsp;@&lt;&lt;&lt;&lt;&lt;&lt;

@#####.##&nbsp;&nbsp;&nbsp;@####.##&nbsp;&nbsp;$ @######.##<BR>

15 $count, $country,&nbsp;&nbsp;$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&nbsp;&nbsp;Country&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Currency&nbsp;&nbsp;Rate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Amount&nbsp;&nbsp;&nbsp;&nbsp;

Value in $<BR>

==============================================================

<BR>

&nbsp;&nbsp;1 UK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pound&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

1.85&nbsp;&nbsp;&nbsp;&nbsp; 100.00&nbsp;&nbsp;$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -