標籤:perl 報表
五一閑暇,看完了一本Perl執行個體精解(第4版)的電子書,這是第19章最後的一個例題。拿來練練手,以犒賞自己所學知識的過程。
下面的源檔案是用來被格式化的檔案:
$ cat xxx Exotic Liquids:Charlotte Cooper:(171) 555-2222New Orleans Cajun Delights:Shelley Burke:(100) 555-4822Grandma Kelly‘s Homestead:Regina Murphy:(313) 555-5735Tokyo Traders:Yoshi Nagase:(03) 3555-5011Cooperativa de Quesos ‘Las Cabras‘:Antonio del Valle Saavedra:(98) 598 76 54Mayumi‘s:Mayumi Ohno:(06) 431-7877Pavlova, Ltd.:Ian Devling:(03) 444-2343Specialty Biscuits, Ltd.:Peter Wilson:(161) 555-4448PB KnSckebrod AB:Lars Peterson:031-987 65 43Refrescos Americanas LTDA:Carlos Diaz:(11) 555 4640Heli Snwaren GmbH & Co. KG:Petra Winkler:(010) 9984510Plutzer LebensmittelgromSrkte AG:Martin Bein:(069) 992755Nord-Ost-Fisch Handelsgesellschaft mbH :Sven Petersen:(04721) 8713Formaggi Fortini s.r.l.:Elio Rossi:(0544) 60323Norske Meierier:Beate Vileid:(0)2-953010Bigfoot Breweries:Cheryl Saylor:(503) 555-9931Svensk Sjofoda AB:Michael Bjorn:08-123 45 67Aux joyeux ecclTsiastiques:GuylFne Nodier:(1) 03.83.00.68 New England Seafood Cannery:Robb Merchant:(617) 555-3267Leka Trading:Chandra Leka:555-8787Lyngbysild:Niels Petersen:43844108Zaanse Snoepfabriek:Dirk Luchte:(12345) 1212Karkki Oy:Anne Heikkonen:(953) 10956G‘day, Mate:Wendy Mackenzie:(02) 555-5914Ma Maison:Jean-Guy Lauzon:(514) 555-9022Pasta Buttini s.r.l.:Giovanni Giudici:(089) 6547665Escargots Nouveaux:Marie Delamare:85.57.00.07Gai pGturage:Eliane Noz:38.76.98.06ForOts 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 |+------------+----------------------------------------+----------------------------+-----------------+
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6C/4E/wKiom1VE5SyyNwyyAAcqTMr_JH0064.jpg" title="perl_picture_line.png" alt="wKiom1VE5SyyNwyyAAcqTMr_JH0064.jpg" />
本文出自 “天道酬勤” 部落格,請務必保留此出處http://lavenliu.blog.51cto.com/5060944/1641337
Perl報表練習-例1