ASP 與 XML--part6 - 聖殿英雄傳說 MUD

Table of Contents

最近在公司要寫的程式,剛剛寫完了,這次有玩到幾個有趣的部份。

首先,設備有設定一年裡面特定的幾天,要套用別的參數,如下..

2012-03-03#
2012-04-04#
2012-05-05#
2012-06-06#

以 asp 語法來說,假設 str 等於上面,那今天假設要做一個日期的
新增,該日期並且要能自己判斷是要塞在上面那個東西的中間、還是
前面、或是後面,該日期假設叫 new_date,它的寫法如下..

new_str = ""
already_ins = 0

' 以 #\n 為分隔符號,對 str 做 explode
for each tmp in split(str,"#" & vbcrlf)

' 進行 new_date 與每個日期的比較, 若小於就加進去
if StrComp(new_date,tmp) < 0 and already_ins = 0 then
new_str = new_str & new_date & "#" & vbcrlf
already_ins = 1
end if

' 然後再累加原先的 tmp
new_str = new_str & tmp & "#" & vbcrlf

next

' 都跑完後若 already_ins 還是 0,就把 new_str 加在最後
if already_ins = 0 then
new_str = new_str & new_date & "#" & vbcrlf
end if

這樣假設輸入的是 2012-05-01,因該日期 > 2012-04-04 以
前,因此在最初跑迴圈時,new_str 都是累加原先的的 tmp.

然後判斷到 05-01 < 05-05 時,new_str 就先加上 05-01,
然後再加上 05-05,這樣就完成了這筆日期的插入動作。

實際的判斷比上面更複雜一點。

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

第二個是,以上面的日期來說,比方有 2011 年、2012 年的
,那我希望它能像底下的呈現方式..

┌─────┬─────┐
│2011-05-05│2012-01-01│
│2011-06-06│2012-02-02│
│ │2012-03-03│
│ │2012-04-04│
│ │2012-05-05│
│ │2012-06-06│
└─────┴─────┘

它的寫法如下..

print_str = "<table><tr><th>"

' 先讀取最開始的 str 的前四個字串, 就是最初年份 2011
years = left(str,4)

for each tmp in split(str,"#" & vbcrlf)

' 當發現有年份跟最初讀到的年份不同時
if years <> left(tmp,4) then

' 馬上更新年份
years = left(tmp,4)
print_str = print_str & tmp & "</th><th>"

else

print_str = print_str & tmp & "<BR>"

end if

next

' 最後再加上結尾

print_str = print_str & "</th></tr></table>"


實際上的程式也比上面複雜,因為該 table 是包在另一
個 table 裡頭,如下..

─┼─────────────┼─
│┌─────┬─────┐│
 ││2011-05-05│2012-01-01││
 ││2011-06-06│2012-02-02││
 ││ │2012-03-03││
 ││ │2012-04-04││
 ││ │2012-05-05││
 ││ │2012-06-06││
 │└─────┴─────┘│
─┼─────────────┼─

不過判斷的原則不變,html 標籤的設計其實跟字串解析
是相同的概念,玩過複雜的字串解析的話,html 標籤的
解析就能駕輕就熟。

比較特別的是因為要讓日期靠左靠上對齊,標籤如下..

<th align=left valign=top>

Laechan

--

All Comments

Damian avatarDamian2012-06-07
理論上因為程式於昨天寫完,今天應該有空coding聖殿.