我正在尝试编写一个脚本来解析试算表。文件中每一行的布局始终相同,但我在让正则表达式正确匹配时遇到问题。该行的前 10 个字符始终是帐号。这是一个例子:
0000000099 S000 Doe, John 00 1,243.22 01/01/1901
我试图将这些中的每一个捕获到一个单独的变量的列中,但我的表达式不起作用。
这是我到目前为止所拥有的。
#!/usr/bin/perl -w
use strict;
my $filename = "S:\\TELLERS\\GalaxyDown\\tbal";
my $answer = undef;
open(FIN, $filename) || die "File not found";
do {
print "Enter an account number: ";
chomp(my $acctNum = <STDIN>);
if ($acctNum =~ /\d{1,10}/) {
$acctNum = pad_zeros($acctNum);
#print "$acctNum\n"; #test to make sure the padding extends the account
#number to 10 digits - comment out after verification
while (<FIN>) {
#print "$_\n";
if (m/(^[0-9]{10}/) {
print "Passed\n";
}
else {
print "Failed\n";
}
}
}
else {
print "Invalid account number. Please try again.\n";
}
print "Would you like to view another account balance? (yes/no): ";
chomp($answer = lc <STDIN>);
} while ($answer ne "no");
sub pad_zeros {
my $optimal_length = 10;
my $num = shift;
$num =~ s/^(\d+)$/("0"x($optimal_length-length$1)).$1/e;
return $num;
}
任何帮助,将不胜感激。