首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

初学者(急):perl访问DB2 导出数据后进行数字和字符串的辨别并将已.开头的数字前面加0 脚本执行结果异常 那位大侠帮修改下 小弟不胜感激!

2012-05-31 
菜鸟求助(急):perl访问DB2 导出数据后进行数字和字符串的辨别并将已.开头的数字前面加0 脚本执行结果错误

菜鸟求助(急):perl访问DB2 导出数据后进行数字和字符串的辨别并将已.开头的数字前面加0 脚本执行结果错误 那位大侠帮修改下 小弟不胜感激!!!
前面一大段是访问数据库的 我测试过了 应该没问题 主要帮看看后面的判断 谢。

Perl code
#!/usr/local/bin/perl -wuse DBI;use strict;# represents a DB2 alias cataloged in your DB2 database directory# my $Database = "dbi:DB2:" + $ARGV[0];my $Database = "dbi:DB2:TEST";# represents the user ID used to connect to the database# my $Username = $ARGV[1];my $Username = "ming";# represents the password for the user ID# my $Password = $ARGV[2];my $Password = "tian";# query statement from database# my $DBQuery = $ARGV[3];my $DBQuery = "SELECT * FROM MONAH.HELP";# represents the database handle returned by the connect statementmy $DBConn = DBI->connect($Database, $Username, $Password) or   die "Cannot connect: $DBI::errstr";# to connect database and prepare the querymy $DBResult = $DBConn->prepare($DBQuery) or   die "Cannot prepare: $DBConn->errstr";# get the result from database$DBResult->execute() or   die "Cannot execute: $DBResult->errstr";#my $RowCount = $DBConn->do($DBQuery);# check the execution's return codemy $DBState = $DBConn->state;my $SQLCode = $DBConn->err;my $Col1;my $Col2;# valibles for test the value typemy $numVal;my $strVal;$DBResult->bind_col(1, \$Col1);$DBResult->bind_col(2, \$Col2);while ($DBResult->fetch) {    if ($Col2 =~ /^\.\d+$/) {       $Col2 = "0" . $Col2;   }    if ($Col2 =~ /^[0-9]+(\.[0-9]{1,100})?$/) {      # valure is a number !       $numVal = $Col2;       $strVal = "NA";   }   else {       # valure is a string !       $numVal = 0;       $strVal = $Col2;   }        print "$Col1|$strVal|$numVal\n";}# finished$DBResult->finish();# close the connection$DBConn->disconnect();


DB2连接应该没问题可以导出数据但是判断后不是想要的结果 本鸟才开始学习perl 希望哪位能帮我改改脚本 非常感谢。

实际输出结果:
Update | 15:32:53 | 0
time | 00:05:00 | 0
CPU used | .476 | 0
capabilit | .3 | 0
perm | 1.304 | 0


想要的结果:
Update | 15:32:53 | 0
time | 00:05:00 | 0
CPU used | NA | 0.476
capabilit | NA | 0.3
perm | NA | 1.304

[解决办法]
while ($DBResult->fetch) {
if ($Col2 =~ /\d*\.\d+/) {
$numVal = $Col2 + 0;
$strVal = "NA";
}
else {
$numVal = 0;
$strVal = $Col2;
}
print "$Col1 | $strVal | $numVal\n";
}

热点排行