我不确定将它们放入单独命名的变量中是最好的选择,但客户总是对的:
use strict;
use warnings;
my $data_read = "T=3420499518 A=914 B=97 C=49 D=436 E=428 F=863 G=34 H=771 I=214 J=493 K=165";
my $data = $data_read;
$data =~ s/[A-KT]=//g;
print "$data\n";
my($T,$A,$B,$C,$D,$E,$F,$G,$H,$I,$J,$K) = split /\s+/, $data;
my $query = "INSERT INTO myhouse (T,A,B,C,D,E,F,G,H,I,J,K) VALUES\n" .
"($T,$A,$B,$C,$D,$E,$F,$G,$H,$I,$J,$K)";
print "$query\n";
print "\nAlternative:\n";
my(@list) = split /\s+/, $data;
print "@list\n";
my $query2 = "INSERT INTO myhouse (T,A,B,C,D,E,F,G,H,I,J,K)\nVALUES(" .
join(",", @list) . ")";
print "$query2\n";
运行此代码段时,它会产生:
3420499518 914 97 49 436 428 863 34 771 214 493 165
INSERT INTO myhouse (T,A,B,C,D,E,F,G,H,I,J,K) VALUES
(3420499518,914,97,49,436,428,863,34,771,214,493,165)
Alternative:
3420499518 914 97 49 436 428 863 34 771 214 493 165
INSERT INTO myhouse (T,A,B,C,D,E,F,G,H,I,J,K)
VALUES(3420499518,914,97,49,436,428,863,34,771,214,493,165)
脚本的后半部分显示了一个更简单的替代方案,它使用一个数组来保存读入的值。
显然,如果您愿意,您可以检查是否为所有变量分配了值以及其他此类错误检查。