Personal notes ~
2008年2月25日 星期一
目前裝的 module
#!/usr/bin/perl
use ExtUtils::Installed;
my $inst = ExtUtils::Installed->new();
my @modules = $inst->modules();
for (sort @modules)
{
print $_,"\n"
}
use ExtUtils::Installed;
my $inst = ExtUtils::Installed->new();
my @modules = $inst->modules();
for (sort @modules)
{
print $_,"\n"
}
2008年2月24日 星期日
Perl 常用的一些函式
push/pop/shift/unshift
將 Array 當作 linked list 處理
push - add values to end of array
pop - remove last value from end of array
unshift - add values to front of array
shift - remove first value from front of array
範例
push(@list, $bar);
push(@list, @rest);
$tos = pop(@list);
while ( $arg = shift(@ARGV) ) { }
unshift( @ARGV, 'zeroth arg', 'first arg');
split/join
split 能夠將一個字串以特定的分隔字串分成許多段, 並傳回一個 Array, 由於這個分隔字串是以正規表示式方式來指定, 使用上非常方便
範例
@list = split(/[, \t]+/, $expr);
範例
while () {
($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(/:/);
}
join 則與 split 功能相反
範例
$line = join(':', $login, $passwd, $uid, $gid, $gcos, $home, $shell);
sort/reverse/grep
sort 用來將一個 array 中的元素以 Ascii 排序後, 傳回一個新的 array
ps: 在利用 sort 在對 array 排序時, 可以自訂排序的方法, 詳細的說明請看這裡
reverse 用來將一個 array 中元素以與原來相反方向排列, 傳回一個新的 array,
reverse 也可以用來將 Hash 中所有的 key 與 hash value 對調
範例
print sort @list;
@list2=reverse sort @list;
grep 會根據給定的一個正規表示式對一個 array 的所有元素作比對, 找出所有比對符合的元素, 組合成一個新的 array 傳回來
@lines=grep(!/^#/, @lines); # delete all lines starting with #
keys/vlaues/each
這三個是專門給 Hash 變數用的函式
keys 傳回一個 hash 中所有的 key 組成的 array
values 傳回一個 hash 中所有的 hash value 組成的 array
each 則是自 hash 中一次抓出一對 (key, hash value) 出來
範例
foreach $key (keys %array) {
printf "%s is %s\n", $key, $array{$key};
}
print reverse sort values %array;
while (($key,$value) = each %array) {
printf "%s is %s\n", $key, $value;
}
chop/chomp
chop 移除字串的最後一個字元, 通常都是用在從檔案輸入一行後, 移除其後的換行字元
chomp 功能與 chop 類似, 但只有當最後一個字元是換行字元時才會將其移除
範例
chop($line);
chop ($host = `hostname`);
while () {
chop;
...
}
sprintf/substr/length/crypt/index/rindex
Perl 上的這些函式基本上用法都和 C language 中同名的函數類似.
詳細的說明請看
sprintf, substr, length, crypt, index, rindex
將 Array 當作 linked list 處理
push - add values to end of array
pop - remove last value from end of array
unshift - add values to front of array
shift - remove first value from front of array
範例
push(@list, $bar);
push(@list, @rest);
$tos = pop(@list);
while ( $arg = shift(@ARGV) ) { }
unshift( @ARGV, 'zeroth arg', 'first arg');
split/join
split 能夠將一個字串以特定的分隔字串分成許多段, 並傳回一個 Array, 由於這個分隔字串是以正規表示式方式來指定, 使用上非常方便
範例
@list = split(/[, \t]+/, $expr);
範例
while (
($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(/:/);
}
join 則與 split 功能相反
範例
$line = join(':', $login, $passwd, $uid, $gid, $gcos, $home, $shell);
sort/reverse/grep
sort 用來將一個 array 中的元素以 Ascii 排序後, 傳回一個新的 array
ps: 在利用 sort 在對 array 排序時, 可以自訂排序的方法, 詳細的說明請看這裡
reverse 用來將一個 array 中元素以與原來相反方向排列, 傳回一個新的 array,
reverse 也可以用來將 Hash 中所有的 key 與 hash value 對調
範例
print sort @list;
@list2=reverse sort @list;
grep 會根據給定的一個正規表示式對一個 array 的所有元素作比對, 找出所有比對符合的元素, 組合成一個新的 array 傳回來
@lines=grep(!/^#/, @lines); # delete all lines starting with #
keys/vlaues/each
這三個是專門給 Hash 變數用的函式
keys 傳回一個 hash 中所有的 key 組成的 array
values 傳回一個 hash 中所有的 hash value 組成的 array
each 則是自 hash 中一次抓出一對 (key, hash value) 出來
範例
foreach $key (keys %array) {
printf "%s is %s\n", $key, $array{$key};
}
print reverse sort values %array;
while (($key,$value) = each %array) {
printf "%s is %s\n", $key, $value;
}
chop/chomp
chop 移除字串的最後一個字元, 通常都是用在從檔案輸入一行後, 移除其後的換行字元
chomp 功能與 chop 類似, 但只有當最後一個字元是換行字元時才會將其移除
範例
chop($line);
chop ($host = `hostname`);
while (
chop;
...
}
sprintf/substr/length/crypt/index/rindex
Perl 上的這些函式基本上用法都和 C language 中同名的函數類似.
詳細的說明請看
sprintf, substr, length, crypt, index, rindex
2008年2月20日 星期三
Regular Expression - Backreferences參考指標
對同一個樣式的比對而言,\1代表第一個符合的項目.\2代表第二個符合的項目.\3代表第三個符合的項目.
ex1.找出tag <TEST></TEST>之間的文字.
ex2.使用$1顯示字串內的數字.
$text = "I have 100 dollars.";
if($text =~ m/(\d+)/){
print "I have $1 dollars.";
}
ex3.使用$1,$2,$3來將字串內的文字顛倒排列.
$text = "he eats fish every day .";
$text =~ s/(\w+) *(\w+) *(\w+)/$3 $2 $1/; //$text =~ s/(\w+) +(\w+) +(\w+)/$3 $2 $1/;
print $text;
//fish eats he every day .
// " *" :代表空白(注意空白)
ex4..
$text = "I have 100 dollars.";
if($text =~ m/(\w*)\W(\w*)\W(\w*)\W(\w*)/){
print @a;
}
ex1.找出tag <TEST></TEST>之間的文字.
$text = <TEST>Application page
go .
</TEST>";
if($text =~ m{<(TEST)>[\w\s.]+<\/\1>}i){
print "Found a title tag.";
}
ex2.使用$1顯示字串內的數字.
$text = "I have 100 dollars.";
if($text =~ m/(\d+)/){
print "I have $1 dollars.";
}
ex3.使用$1,$2,$3來將字串內的文字顛倒排列.
$text = "he eats fish every day .";
$text =~ s/(\w+) *(\w+) *(\w+)/$3 $2 $1/; //$text =~ s/(\w+) +(\w+) +(\w+)/$3 $2 $1/;
print $text;
//fish eats he every day .
// " *" :代表空白(注意空白)
ex4..
$text = "I have 100 dollars.";
if($text =~ m/(\w*)\W(\w*)\W(\w*)\W(\w*)/){
print @a;
}
Regular Expression - Assertion
^ : 符合行的開頭.
$ : 符合行的結尾,或是在結尾的換行前.
\b : 符合一個單字邊界(word boundary).
\B : 符合一個非單字邊界.
\A : 只符合字串的開頭.
\Z : 只符合字串的結尾,或是在結尾的換行前.
\z : 只符合字串的結尾.
\G : 只符合上一個 m//g 所遺留者.
(?=EXPR) : 如果EXPR符合下一個就符合.
(?!EXPR) : 如果EXPR不符合下一個就符合.
(?<=EXPR) : 如果EXPR符合上一個就符合.
(?< !EXPR) : 如果EXPR不符合下一個就符合.
ex1.只有單行輸入 只有"quit" 才能離開
while(<>){
if(m/^(quit)$/){exit;}
}
2008年2月19日 星期二
Regular Expression - 字元類別(character class)
以[]來包含一個字元類別.
以 - 來表示一個範圍.
^ : 反集合
\s : whitespace
[^a-zA-Z\s] : 不是字母(A-Za-z)與空白(\s)的字元.
$_ : m//,s/// 預設作用物件
| : (alternative match patterns)多選一的符合樣式,like or.
數量元(quantifier),指定樣式比對一定的數目.
ex1.將不是大寫字母開頭的單字,存成陣列.
$_ = "Perl is the best language.";
@a = m/\b[^A-Z]+\b/g;
print @a;
ex2. 讀入quit,exit,stop 程式終止
while(<>){
if(m/quit|exit|stop/){exit;}
}
ex3.將一個或一個以上的空白,以一個空白來取代
$text = "Hello form Perl.";
$text=~s/ +/ /g;
print $text;
以 - 來表示一個範圍.
^ : 反集合
\s : whitespace
[^a-zA-Z\s] : 不是字母(A-Za-z)與空白(\s)的字元.
$_ : m//,s/// 預設作用物件
| : (alternative match patterns)多選一的符合樣式,like or.
數量元(quantifier),指定樣式比對一定的數目.
max min
+ +? 符合一次 或多次
* *? 符合零次 或多次
? ?? 符合零次 或一次
ex1.將不是大寫字母開頭的單字,存成陣列.
$_ = "Perl is the best language.";
@a = m/\b[^A-Z]+\b/g;
print @a;
ex2. 讀入quit,exit,stop 程式終止
while(<>){
if(m/quit|exit|stop/){exit;}
}
ex3.將一個或一個以上的空白,以一個空白來取代
$text = "Hello form Perl.";
$text=~s/ +/ /g;
print $text;
Reqular Expression - Function
quotemeta : 每個非英數字元前加上一個\.
ex.
$text = quotemeta('I said "Hello."');
print $text;
//I\ said\ \"Hello\.\";
ex.
$text = quotemeta('I said "Hello."');
print $text;
//I\ said\ \"Hello\.\";
2008年2月18日 星期一
Regular Expression - 運算子
pattern match 樣式比對(PERL)
m// (match operator) 搜尋符合Regular Expression 的字串或陳述式.
m/PATTERN/
; "/"=>分界元delimiter ,if use "/" => "m" can be abridged.
=> ex : if($text !~ /exit/i){}
can use other delimiter ex: m()、m[]、m{}、m<> , but "m" can't be abridged .
*pattern modifier 樣式修正元
=>一序列的"選項",放置在 regular expression last delimiter(分界元)之後.改變搜尋方法與解析方式.
/i => 忽略大小寫
/m => 從"行"開頭開始比對
^ => 從"字串"開頭開始比對 m/^/
/o
/s
/x
/g =>傳回所有符合的比對,而不只是第一個.
s/// (substitution operator)以新字串取代符合Regular Expression 的字串或陳述式.
s/PATTERN/REPLACEMENT/
tr/// (translation operator)只能作用在純量,或陣列與雜湊內的單一元素.
tr/SEARCHLIST/REPLACEMENTLIST/
delimiter of SEARCHLSIT can be not the same as one of REPLACEMENTLIST.
ex. $string =~ tr(:)[/];
/c =>SEARCHLIST的補集合
/d =>刪除 CHAR NOT IN REPLACEMENTLIST
/s =>重複的字元縮成一個
ex1.從鍵盤內讀入輸入文字,如果不論大小寫的"exit"輸入,則結束.
print "Enter a text:\n";
while(<>){
if(m/exit/i){exit;}
print "Enter a text : \n";
}
ex2.傳回所有符合四個字元的單字串列.
符合運算子的傳回值 = 符合運算子傳回取代的字串.
@a = ("There are many cats." =~ m/\b\w{4,4}\b/g);
print "@a";
// many cats
/b=>單字邊界
/w=>尋找一個英數字元
{4,4}=>最少4個字元,最多4個字元
ex3.以dog 取代 cat
$text = "I have a dog.";
$text =~ s/dog/cat/;
print $text;
ex4 取代運算子的傳回值 = 取代運算子傳回取代的數目.
$string =" TEXT is the uppercase of text";
$num = $string =~ s{text}{book}gi; //($num = $string) =~ s{text}{book}gi => will be $string value , not return value.
print $num; //2
print $string; //book is the uppercase of book
ex5.將字串$string 內所有的小寫字元改成大寫.
$string = "Basketball is my favorite sport.";
$string =~ tr/a-z/A-Z/;
print $string;
//BASKETBALL IS MY FAVORTIE SPORT.
ex6.將$text的 所有非字母(a-z A-Z)字元,以"-"來取代.
$string = "Basketball is my favorite sport."
$string =~ tr/a-zA-Z/-/c;
print $string;
//Basketball-is-my-favorite-sport
ex7.將 $string 內的 "\n" 字元刪除
$string = "Basketball \n is \n my \n favorite \n sport\n.";
$string =~ tr/\n//d;
print $string;
//Basketball is my favorite sport.
ex8.將重複的字元歸為單一個.
$string = "good book ball baa seek haaaa";
$string =~ tr/a-z/a-z/s;
print $string;
// god bok bal ba sek ha
ex9.計算$string 非英數字元數目.
$string = "Basketball is my favorite sport.";
$num = $string =~ tr/a-zA-Z0-9//cs;
print $string;
print $num;
//
ex10.\w符合一個字元,\;脫逸出".";\w+符合一個單字
$string = "This is a house.";
$string =~ s/\w+/There/g;
print $string;
// There There There There
ex11. 取代第一個符合的 英數字元開頭的字串
$text = "Java language is the best.";
$text =~ s/[A-Za-z]+/PERL/;
print $text;
//Perl language is the best.
"^": 反集合
$text =~ s/[^A-Za-z]+/PERL/;
print $text;
//JavaPerllanguage is the best.
m// (match operator) 搜尋符合Regular Expression 的字串或陳述式.
m/PATTERN/
; "/"=>分界元delimiter ,if use "/" => "m" can be abridged.
=> ex : if($text !~ /exit/i){}
can use other delimiter ex: m()、m[]、m{}、m<> , but "m" can't be abridged .
*pattern modifier 樣式修正元
=>一序列的"選項",放置在 regular expression last delimiter(分界元)之後.改變搜尋方法與解析方式.
/i => 忽略大小寫
/m => 從"行"開頭開始比對
^ => 從"字串"開頭開始比對 m/^/
/o
/s
/x
/g =>傳回所有符合的比對,而不只是第一個.
s/// (substitution operator)以新字串取代符合Regular Expression 的字串或陳述式.
s/PATTERN/REPLACEMENT/
tr/// (translation operator)只能作用在純量,或陣列與雜湊內的單一元素.
tr/SEARCHLIST/REPLACEMENTLIST/
delimiter of SEARCHLSIT can be not the same as one of REPLACEMENTLIST.
ex. $string =~ tr(:)[/];
/c =>SEARCHLIST的補集合
/d =>刪除 CHAR NOT IN REPLACEMENTLIST
/s =>重複的字元縮成一個
ex1.從鍵盤內讀入輸入文字,如果不論大小寫的"exit"輸入,則結束.
print "Enter a text:\n";
while(<>){
if(m/exit/i){exit;}
print "Enter a text : \n";
}
ex2.傳回所有符合四個字元的單字串列.
符合運算子的傳回值 = 符合運算子傳回取代的字串.
@a = ("There are many cats." =~ m/\b\w{4,4}\b/g);
print "@a";
// many cats
/b=>單字邊界
/w=>尋找一個英數字元
{4,4}=>最少4個字元,最多4個字元
ex3.以dog 取代 cat
$text = "I have a dog.";
$text =~ s/dog/cat/;
print $text;
ex4 取代運算子的傳回值 = 取代運算子傳回取代的數目.
$string =" TEXT is the uppercase of text";
$num = $string =~ s{text}{book}gi; //($num = $string) =~ s{text}{book}gi => will be $string value , not return value.
print $num; //2
print $string; //book is the uppercase of book
ex5.將字串$string 內所有的小寫字元改成大寫.
$string = "Basketball is my favorite sport.";
$string =~ tr/a-z/A-Z/;
print $string;
//BASKETBALL IS MY FAVORTIE SPORT.
ex6.將$text的 所有非字母(a-z A-Z)字元,以"-"來取代.
$string = "Basketball is my favorite sport."
$string =~ tr/a-zA-Z/-/c;
print $string;
//Basketball-is-my-favorite-sport
ex7.將 $string 內的 "\n" 字元刪除
$string = "Basketball \n is \n my \n favorite \n sport\n.";
$string =~ tr/\n//d;
print $string;
//Basketball is my favorite sport.
ex8.將重複的字元歸為單一個.
$string = "good book ball baa seek haaaa";
$string =~ tr/a-z/a-z/s;
print $string;
// god bok bal ba sek ha
ex9.計算$string 非英數字元數目.
$string = "Basketball is my favorite sport.";
$num = $string =~ tr/a-zA-Z0-9//cs;
print $string;
print $num;
//
ex10.\w符合一個字元,\;脫逸出".";\w+符合一個單字
$string = "This is a house.";
$string =~ s/\w+/There/g;
print $string;
// There There There There
ex11. 取代第一個符合的 英數字元開頭的字串
$text = "Java language is the best.";
$text =~ s/[A-Za-z]+/PERL/;
print $text;
//Perl language is the best.
"^": 反集合
$text =~ s/[^A-Za-z]+/PERL/;
print $text;
//JavaPerllanguage is the best.
訂閱:
文章 (Atom)
追蹤網誌清單
IT Info
Reference
標籤
- 代討論
- 防盜
- 常規表示式(regular expression)
- 進度
- 電子地圖
- 網站搬家程式
- Ajax Library
- Application of Perl
- C#
- CakePHP
- Coding Convention
- CSS
- Data injection
- DBI
- Debug
- Diff Browser
- Effect : Slideshow
- Engineering - Desing Pattern
- framework
- Framework - CodeIgniter
- Functions
- Imgick
- jquery
- JS Framework
- JS Library
- JS Object : Statement
- Linux
- Mapping Other Language
- Need to Turnning
- NetBeans
- OOAD-MVC
- open source suit software
- Performance
- Perl Installation
- PHP Class
- PHP rpc Java
- PHP to PERL
- Presentation
- RIA
- Serculity
- Service
- SQL injection
- Tips
- Tools
- Variable
- Versus
- WCF
- WebSite Thumbnail
- XML
- ZK Ajax