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

luoxinup luoxinup 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
前面一大段是访问数据库的 我测试过了 应该没问题 主要帮看看后面的判断 谢。
#!/usr/local/bin/perl -w
use 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 statement
my $DBConn = DBI->connect($Database, $Username, $Password) or
die "Cannot connect: $DBI::errstr";

# to connect database and prepare the query
my $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 code
my $DBState = $DBConn->state;
my $SQLCode = $DBConn->err;

my $Col1;
my $Col2;

# valibles for test the value type
my $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
...全文
评论 7
收藏 已收藏
分享
返回顶部
luoxinup
脚本语言 37,738 成员 34,208 内容