第一个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);