開発環境
- OS X Lion - Apple(OS)
- Apache (Web Server)
- PHP (サーバーサイドプログラミング言語、スクリプト言語)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
『初めてのPHP5』 (David Sklar 著、 桑村 潤 翻訳、 廣川 類 翻訳、 オライリー・ジャパン、2005年、ISBN978-4-87311-257-2)の第 4章(配列の操作)4.7(演習問題)1.を解いてみる。
1.
HTML、PHPのソースコード(BBEdit)
<?php $people = array("New York, NY" => 8008278, "Los Angeles, CA" => 3694820, "Chicago, IL" => 2896016, "Houston, TX" => 1953631, "Philadelphia, PA" => 1517550, "Phoenix, AZ" => 1321045, "San Diego, CA" => 1223400, "Dallas, TX" => 1188580, "San Antonio, TX" => 1144646, "Detroit, MI" => 951270); print '<table border=1 width=350>'; print '<caption>2000年のアメリカの10大都市の(人口規模)</caption>'; print '<tr align=center ><th>都市</th><th>人口</th></tr>'; $total = 0; foreach ($people as $key => $value) { print '<tr><td align=left>' . $key . '</td><td align=right >' . $value . '</td></tr>'; $total += $value; } print '<tr><th align=center>合計</th><th align=right>' . $total . '</th></tr>'; print '</table>'; ?>
ちなみにJavaScriptの場合。
コード(BBEdit)
var people = [ ["New York, NY" , 8008278], ["Los Angeles, CA" , 3694820], ["Chicago, IL" , 2896016], ["Houston, TX" , 1953631], ["Philadelphia, PA" , 1517550], ["Phoenix, AZ" , 1321045], ["San Diego, CA" , 1223400], ["Dallas, TX" , 1188580], ["San Antonio, TX" , 1144646], ["Detroit, MI" , 951270] ]; var result = '<table border=1 width=350>' + '<caption>2000年のアメリカの10大都市の(人口規模)</caption>' + '<tr align=center ><th>都市</th>' + '<th>人口</th></tr>'; var total = 0; var i; for(i = 0; i < people.length; i++){ result += '<tr><td align=left>' + people[i][0] + '</td><td align=right >' + people[i][1] + '</td></tr>'; total += people[i][1]; } result += '<tr><th align=center>合計</th><th align=right>' + total + '</th></tr></table>'; $('#d0').html(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- people = [ ("New York, NY", 8008278), ("Los Angeles, CA", 3694820), ("Chicago, IL", 2896016), ("Houston, TX", 1953631), ("Philadelphia, PA", 1517550), ("Phoenix, AZ", 1321045), ("San Diego, CA", 1223400), ("Dallas, TX", 1188580), ("San Antonio, TX", 1144646), ("Detroit, MI", 951270)] print("city".center(20), "people".center(10), sep="") for c, p in people: print(c.ljust(20), str(p).rjust(10), sep="")
入出力結果(Terminal)
$ ./sample.py city people New York, NY 8008278 Los Angeles, CA 3694820 Chicago, IL 2896016 Houston, TX 1953631 Philadelphia, PA 1517550 Phoenix, AZ 1321045 San Diego, CA 1223400 Dallas, TX 1188580 San Antonio, TX 1144646 Detroit, MI 951270 $
0 コメント:
コメントを投稿