每月彙整: 2007 年 4 月

誤刪 /var/db/pkg 的解救方法!!!

剛剛在irc 看到的~~備忘一下
(17:54:34) weithenn: 請教長輩手賤把 /var/db/pkg 砍了如何 rebuild?
(17:55:18) S3pPoor: 到 /usr/ports/ 裡面開始重新裝… XD
(17:55:49) weithenn: S3pPoor:需要的在重裝一次?
(17:55:54) tys0n: … //pat
(17:55:59) ***S3pPoor 雙手合十,喃喃唸著「阿彌陀佛」。
(17:56:21) S3pPoor: weithenn: /var/db/pkg 裡面的東西沒有第二份啊, 所以就…
(17:56:44) Jedi_: 怎麼辦喔,就從備援機上面 co 回來
(17:56:46) weithenn: S3pPoor:了解了,手賤是要付出代價的
(17:56:48) ***Jedi_ flee
(17:57:14) weithenn: Jedi_:大師說的是啊
(17:57:37) Jedi_: 不過我自己也沒有備援機,嗚
(17:57:49) Jedi_: 我只有松果有做 A/B Backup
(17:57:56) ***weithenn 這台也沒有
(17:58:02) ***weithenn
(17:58:18) Jedi_: 所以我們還是需要 versioned fs
(17:58:38) ***S3pPoor ╮(╯OO╰)╭
(17:59:06) ***weithenn 下次手要先洗乾淨
(18:02:41) chinsan: 先裝上 portupgrade ,然後 mkdir -p /var/db/pkg ; pkgdb -Fu
(18:04:17) plasmaball: chinsan: 好強
(18:06:19) weithenn: chinsan:我試試在來回報
(18:06:30) chinsan: plasmaball: 漿伯長輩,您想太多 @_@
(18:09:07) psilotum 離開聊天室 (quit: “leaving”)。
(18:35:10) candyz [~candyz@dns.kandix.idv.tw] 進入聊天室。
(18:35:59) ***weithenn 回來不少,有些還是漏了
(18:36:07) ***weithenn 在觀察看看吧
:(h):

MXL DB災難!!!!

下午4-5時許,lxz傳mms 給我
說mxl 的db 發生了些許的問題,造成了所有的topic title 都只變成20的length
幸虧在另一台機器上有備份……
不過是一個月前的…且是UTF-8的
下班馬上衝回家~~~~把資料抓出來iconv再倒回去

跑了一個多小時,剛剛看起來是還滿正常的….
等lyz 明天確認~~

Regexp (zero-width negative look-ahead)

剛剛在irc 看到的
(22:09:12) stormax: ‹一個正規式不知道怎樣寫, 我想搜尋 <? 但是要排除 <?php
(22:09:21) stormax: 有長輩知道怎麼寫嗎?
.
.
.
(22:31:19) mhsin: stormax: <?(!php) # 22:09 < stormax> �一個正規式不知道怎樣寫, 我想搜尋 < ? 但是要排除
(22:31:19) yinjieh: 那我會 < ?php
(22:31:33) yinjieh: 再 phpphp -> php
(22:31:53) mhsin: stormax: (!something) 叫 zero-width negative look-ahead
(22:32:01) mhsin: 啊錯了
(22:32:06) mhsin: (?!something) 才對
(22:32:24) mhsin: aaa(?!bbb) 就會 match 「後面不是 bbb 的 aaa」
然後去google 了一下
就找到一篇
Perl Regular Expressions Tip Sheet
裡面就有講到
zero-width negative look-ahead
我也去grep 實做了一下~~
還真的work

$ grep -P ‘aaa(?!bbb)’ ok.php
看irc 還真不少東西 ::-p:

UPDATE:2007/11/08
新增有關VIM 上的用法

							*/zero-width*
	When using "@=" (or "^", "$", "<", ">") no characters are included
	in the match.  These items are only used to check if a match can be
	made.  This can be tricky, because a match with following items will
	be done in the same position.  The last example above will not match
	"foobarfoo", because it tries match "foo" in the same position where
	"bar" matched.

	Note that using "&" works the same as using "@=": "foo&.." is the
	same as "(foo)@=..".  But using "&" is easier, you don't need the
	braces.

							*/@!*
@!	Matches with zero width if the preceding atom does NOT match at the
	current position. |/zero-width| {not in Vi}
	Like '(?!pattern)" in Perl.
	Example			matches 
	foo(bar)@!		any "foo" not followed by "bar"
	a.{-}p@!		"a", "ap", "app", etc. not followed by a "p"
	if ((then)@!.)*$	"if " not followed by "then"

	Using "@!" is tricky, because there are many places where a pattern
	does not match.  "a.*p@!" will match from an "a" to the end of the
	line, because ".*" can match all characters in the line and the "p"
	doesn't match at the end of the line.  "a.{-}p@!" will match any
	"a", "ap", "aap", etc. that isn't followed by a "p", because the "."
	can match a "p" and "p@!" doesn't match after that.

	You can't use "@!" to look for a non-match before the matching
	position: "(foo)@!bar" will match "bar" in "foobar", because at the
	position where "bar" matches, "foo" does not match.  To avoid matching
	"foobar" you could use "(foo)@!...bar", but that doesn't match a
	bar at the start of a line.  Use "(foo)@<!bar".

							*/@<=*
@<=	Matches with zero width if the preceding atom matches just before what
	follows. |/zero-width| {not in Vi}
	Like '(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns.
	Example			matches 
	(an_s+)@<=file	"file" after "an" and white space or an
				end-of-line
	For speed it's often much better to avoid this multi.  Try using "zs"
	instead |/zs|.  To match the same as the above example:
		an_s+zsfile

	"@<=" and "@<!" check for matches just before what follows.
	Theoretically these matches could start anywhere before this position.
	But to limit the time needed, only the line where what follows matches
	is searched, and one line before that (if there is one).  This should
	be sufficient to match most things and not be too slow.
	The part of the pattern after "@<=" and "@<!" are checked for a
	match first, thus things like "1" don't work to reference () inside
	the preceding atom.  It does work the other way around:
	Example			matches 
	1@<=,([a-z]+)	",abc" in "abc,abc"

							*/@<!*
@<!	Matches with zero width if the preceding atom does NOT match just
	before what follows.  Thus this matches if there is no position in the
	current or previous line where the atom matches such that it ends just
	before what follows.  |/zero-width| {not in Vi}
	Like '(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns.
	The match with the preceding atom is made to end just before the match
	with what follows, thus an atom that ends in ".*" will work.
	Warning: This can be slow (because many positions need to be checked
	for a match).
	Example			matches 
	(foo)@<!bar		any "bar" that's not in "foobar"
	(//.*)@<!in	"in" which is not after "//"

							*/@>*
@>	Matches the preceding atom like matching a whole pattern. {not in Vi}
	Like '(?>pattern)" in Perl.
	Example		matches 
	(a*)@>a	nothing (the "a*" takes all the "a"'s, there can't be
			another one following)

	This matches the preceding atom as if it was a pattern by itself.  If
	it doesn't match, there is no retry with shorter sub-matches or
	anything.  Observe this difference: "a*b" and "a*ab" both match
	"aaab", but in the second case the "a*" matches only the first two
	"a"s.  "(a*)@>ab" will not match "aaab", because the "a*" matches
	the "aaa" (as many "a"s as possible), thus the "ab" can't match.

程式的功力差別!!!!

今天在寫apollo的ad 部份
有一個部份是在實做百面骰的部份
用百面骰的概念來實做百分比的呈現
例如我有5個值
a 的 比例是20,
b 的 比例是30,
c 的 比例是50,
d 的 比例是80,
f 的 比例是160
然後依比例來讓他們呈現應有的次數
最直接想的是
直接塞個20+30+50+80+160 的array
不過覺得這樣做實在是挺白痴的
覺得應該有更好的方法
經過”貓哥”的小小提示”區間”
我想到的是
$rand = rand(1,$ad_total);
用for 來做
if (0 <= $rand<= a){ return a; } elseif ( a < $rand <=b ){ return b; } 一直下去。 不過貓哥寫的是這樣 $rand = rand(1,$ad_total); for ($i=0; $i