五一闲暇,看完了一本Perl实例精解(第4版)的电子书,这是第19章最后的一个例题。拿来练练手,以犒赏自己所学知识的过程。
下面的源文件是用来被格式化的文件:
$ cat xxx Exotic Liquids:Charlotte Cooper:(171) 555-2222 New Orleans Cajun Delights:Shelley Burke:(100) 555-4822 Grandma Kelly‘s Homestead:Regina Murphy:(313) 555-5735 Tokyo Traders:Yoshi Nagase:(03) 3555-5011 Cooperativa de Quesos ‘Las Cabras‘:Antonio del Valle Saavedra:(98) 598 76 54 Mayumi‘s:Mayumi Ohno:(06) 431-7877 Pavlova, Ltd.:Ian Devling:(03) 444-2343 Specialty Biscuits, Ltd.:Peter Wilson:(161) 555-4448 PB KnSckebrod AB:Lars Peterson:031-987 65 43 Refrescos Americanas LTDA:Carlos Diaz:(11) 555 4640 Heli Snwaren GmbH & Co. KG:Petra Winkler:(010) 9984510 Plutzer LebensmittelgromSrkte AG:Martin Bein:(069) 992755 Nord-Ost-Fisch Handelsgesellschaft mbH :Sven Petersen:(04721) 8713 Formaggi Fortini s.r.l.:Elio Rossi:(0544) 60323 Norske Meierier:Beate Vileid:(0)2-953010 Bigfoot Breweries:Cheryl Saylor:(503) 555-9931 Svensk Sjofoda AB:Michael Bjorn:08-123 45 67 Aux joyeux ecclTsiastiques:GuylFne Nodier:(1) 03.83.00.68 New England Seafood Cannery:Robb Merchant:(617) 555-3267 Leka Trading:Chandra Leka:555-8787 Lyngbysild:Niels Petersen:43844108 Zaanse Snoepfabriek:Dirk Luchte:(12345) 1212 Karkki Oy:Anne Heikkonen:(953) 10956 G‘day, Mate:Wendy Mackenzie:(02) 555-5914 Ma Maison:Jean-Guy Lauzon:(514) 555-9022 Pasta Buttini s.r.l.:Giovanni Giudici:(089) 6547665 Escargots Nouveaux:Marie Delamare:85.57.00.07 Gai pGturage:Eliane Noz:38.76.98.06 ForOts d‘Trables:Chantal Goulet:(514) 555-2955
这个文件是自己所写的代码文件:
$ cat example19.pl open(FH, "<xxx") or die "Can‘t open xxx: $!\n"; $headline = "+" . "-" x 12 . "+" . "-" x 40 . "+". "-" x 28 . "+" . "-" x 17 . "+"; $field1 = ">" x 9; $field2 = "<" x 37; $field3 = "<" x 25; $field4 = "<" x 14; $format_top = qq( format STDOUT_TOP = $headline | supplierid | companyname | contactname | phone | $headline . ); $format = qq( format STDOUT = | \@$field1 | \@$field2 | \@$field3 | \@$field4 | \$., \$compayname, \$contactname, \$phone . ); eval $format_top; eval $format; while (<FH>) { ($compayname, $contactname, $phone) = split(/:/); write; } close(FH); print $headline, "\n";
这是运行后的结果:
$ perl example19.pl +------------+----------------------------------------+----------------------------+-----------------+ | supplierid | companyname | contactname | phone | +------------+----------------------------------------+----------------------------+-----------------+ | 1 | Exotic Liquids | Charlotte Cooper | (171) 555-2222 | | 2 | New Orleans Cajun Delights | Shelley Burke | (100) 555-4822 | | 3 | Grandma Kelly‘s Homestead | Regina Murphy | (313) 555-5735 | | 4 | Tokyo Traders | Yoshi Nagase | (03) 3555-5011 | | 5 | Cooperativa de Quesos ‘Las Cabras‘ | Antonio del Valle Saavedra | (98) 598 76 54 | | 6 | Mayumi‘s | Mayumi Ohno | (06) 431-7877 | | 7 | Pavlova, Ltd. | Ian Devling | (03) 444-2343 | | 8 | Specialty Biscuits, Ltd. | Peter Wilson | (161) 555-4448 | | 9 | PB KnSckebrod AB | Lars Peterson | 031-987 65 43 | | 10 | Refrescos Americanas LTDA | Carlos Diaz | (11) 555 4640 | | 11 | Heli Snwaren GmbH & Co. KG | Petra Winkler | (010) 9984510 | | 12 | Plutzer LebensmittelgromSrkte AG | Martin Bein | (069) 992755 | | 13 | Nord-Ost-Fisch Handelsgesellschaft mbH | Sven Petersen | (04721) 8713 | | 14 | Formaggi Fortini s.r.l. | Elio Rossi | (0544) 60323 | | 15 | Norske Meierier | Beate Vileid | (0)2-953010 | | 16 | Bigfoot Breweries | Cheryl Saylor | (503) 555-9931 | | 17 | Svensk Sjofoda AB | Michael Bjorn | 08-123 45 67 | | 18 | Aux joyeux ecclTsiastiques | GuylFne Nodier | (1) 03.83.00.68 | | 19 | New England Seafood Cannery | Robb Merchant | (617) 555-3267 | | 20 | Leka Trading | Chandra Leka | 555-8787 | | 21 | Lyngbysild | Niels Petersen | 43844108 | | 22 | Zaanse Snoepfabriek | Dirk Luchte | (12345) 1212 | | 23 | Karkki Oy | Anne Heikkonen | (953) 10956 | | 24 | G‘day, Mate | Wendy Mackenzie | (02) 555-5914 | | 25 | Ma Maison | Jean-Guy Lauzon | (514) 555-9022 | | 26 | Pasta Buttini s.r.l. | Giovanni Giudici | (089) 6547665 | | 27 | Escargots Nouveaux | Marie Delamare | 85.57.00.07 | | 28 | Gai pGturage | Eliane Noz | 38.76.98.06 | | 29 | ForOts d‘Trables | Chantal Goulet | (514) 555-2955 | +------------+----------------------------------------+----------------------------+-----------------+
本文出自 “天道酬勤” 博客,请务必保留此出处http://lavenliu.blog.51cto.com/5060944/1641337
原文地址:http://lavenliu.blog.51cto.com/5060944/1641337