新手菜鸟再次厚颜求助:怎样在循环体内做两列数据求和及去掉单位符号

luoxinup 2012-05-29 10:21:07
小弟再次厚颜救助。。。
使用perl语言将DB2数据库表中有两列数据 目的是把表中第二列数据中的字符串和数值分开分别放在第二列和第三列中。 这个昨天bugs2k 已经在下面的代码中帮小弟解决了。现在在前面的基础上如果想先将单位"M"去除 然后将testB和testC 的数值型数据相加并将结果放在第四列中.....如何解决?? 感谢各位大侠 帮看看 。。

原始数据:
testA | luck
testB | 0.54M
testC | 7.83M
testD | .43M

想要得到的数据:

testA | luck | 0 | 0
testB | NA | 0.54 | 0
testC | NA | 7.83 | 8.37
testD | NA | 0.43 | 0


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";

}
...全文
147 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoxinup 2012-05-30
  • 打赏
  • 举报
回复
首先感谢bugs2k两次的帮助 上次那个脚本测试过后发现输出整数后 脚本将其归类为字符型,按照您的方法稍微改动了一下现在已经可以应用了。。感谢! 这次上面的代码我测试过没有问题。。但是俺真的是看不懂(本人接触perl共4天,纯是被逼无奈..) 我直接讲吧 那个句柄__DATA__在我看来等于是数据源 但实际环境是从DB2导出的数据 放在变量 $Col1 $Col2 中 用while 循环控制。。。悲剧的是我不知道用什么方法将您的脚本修改后加载到实际脚本中,(掩面痛哭!)我今天整到现在也没整明白,如果可以 您能不能再帮我看看能不能直接在实际脚本中修改(就是数据源是DB2导出的数据那种情况)。万分感谢!!!!!!!

#!/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) {

。。。。。。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。。。。。。


}

# finished
$DBResult->finish();

# close the connection
$DBConn->disconnect();


luoxinup 2012-05-30
  • 打赏
  • 举报
回复
已经用其他方法搞定了 还是感谢你。。
bugs2k 2012-05-29
  • 打赏
  • 举报
回复
#/usr/bin/env perl

use strict;
use warnings;

my @datas = ();
foreach my $line (<DATA>) {
my ($name, $data) = split /\s+/, $line;
my ($numVal, $strVal) = ();
if ($data =~ /(\d*\.\d+)/) {
$numVal = $1 + 0;
$strVal = 'NA';
}
else {
$numVal = 0;
$strVal = $data;
}
push @datas, [$name, $strVal, $numVal, 0];
}

my $testB = $datas[1];
my $testC = $datas[2];
$testC->[3] += $testB->[2] + $testC->[2];

foreach my $data (@datas) {
print "$data->[0] | $data->[1] | $data->[2] | $data->[3]\n";
}

__DATA__
testA luck
testB 0.54M
testC 7.83M
testD .43M

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧