第一個perl指令碼是同事幫忙寫的。這個功能用JAVA實現後,中途不拋異常退出;用perl後,正常。
同事幫忙寫,我自己修改調試通過,竟然對perl有了一定的瞭解。寫下來,備忘,說不定今天是個新
的開始。
#!/usr/bin/perl
use DBI;
#串連資料庫,就是這麼方便
$dbh = DBI->connect("DBI:mysql:newWap:172.16.58.36", "root","");
#perl io
open (F, "data.txt");
open (OUT, ">>result.txt");
#這裡定義的是hashtable,與我們平常用的數組不一樣,在這裡比數組方便了很多
local (%all_count);
local (%db_count);
local (%qqs);
$sql = $dbh->prepare("select user_id from t_core_user");
$sql->execute();
#@表示數組
while (@qqid = $sql->fetchrow_array) {
$qqs{$qqid[0]} = "0";
}
print ("DB operation completed./n");
while ($str = <F>) {
chomp($str);
#print ($str);
$ch = substr($str, 0, 1);
if ($ch eq "F") {
next;
}
@arr = split(//t/, $str);
$qq = $arr[0];
$time = $arr[2];
if (length($time) < 6) {
next;
}
#print ("qq : $qq, time : $time/n");
if (substr($arr[2], 0, 6) eq "200505") {
$all_count{"5"} ++;
if (exists($qqs{$qq})) {
$db_count{"5"} ++;
}
}
if (substr($arr[2], 0, 6) eq "200506") {
$all_count{"6"} ++;
if (exists($qqs{$qq})) {
$db_count{"6"} ++;
}
}
if (substr($arr[2], 0, 6) eq "200507") {
$all_count{"7"} ++;
if (exists($qqs{$qq})) {
$db_count{"7"} ++;
}
}
}
print OUT ("6 : ".$all_count{"5"}." , ".$db_count{"5"}."/n");
print OUT ("6 : ".$all_count{"6"}." , ".$db_count{"6"}."/n");
print OUT ("6 : ".$all_count{"7"}." , ".$db_count{"7"}."/n");
close(F);
close(OUT);