我正在尝试解析csv文件以执行简单的操作:提取姓氏,ID和生日,并将生日格式从m / d / yyyy更改为yyyymmdd。
(1)我在生日时使用了命名捕获,但似乎没有调用命名的捕获方法来制作我想要的东西。
(2)继承语法动作方法似乎不适用于命名捕获。
我做错了什么?
my $x = "1,,100,S113*L0,35439*01,John,JOE,,,03-10-1984,47 ELL ST #6,SAN FRANCISCO,CA,94112,415-000-0000,,5720,Foo Bar,06-01-2016,06-01-2016,Blue Cross,L,0,0";
# comma separated lines
grammar insurCommon {
regex aField { <-[,]>*? }
regex theRest { .* }
}
grammar insurFile is insurCommon {
regex TOP { <aField> \,\, # item number
<aField> \, # line of business
<aField> \, # group number
<ptID=aField> \, # insurance ID,
<ptLastName=aField> \, # last name,
<aField> \,\,\, # first name
<ptDOB=aField> \, # birthday
<theRest> }
}
# change birthday format from 1/2/3456 to 34560102
sub frontPad($withWhat, $supposedStrLength, $strToPad) {
my $theStrLength = $strToPad.chars;
if $theStrLength >= $supposedStrLength { $strToPad; }
else { $withWhat x ($supposedStrLength - $theStrLength) ~ $strToPad; }
}
class dateAct {
method reformatDOB($aDOB) {
$aDOB.Str.split(/\D/).map(frontPad("0", 2, $_)).rotate(-1).join;
}
}
class insurFileAct is dateAct {
method TOP($anInsurLine) {
my $insurID = $anInsurLine<ptID>.Str;
my $lastName = $anInsurLine<ptLastName>.Str;
my $theDOB = $anInsurLine<ptDOB>.made; # this is not made;
$anInsurLine.make("not yet made"); # not yet getting $theDOB to work
}
method ptDOB($DOB) { # ?ptDOB method is not called by named capture?
my $newDOB = reformatDOB($DOB); # why is method not inherited
$DOB.make($newDOB);
}
}
my $insurAct = insurFileAct.new;
my $m = insurFile.parse($x, actions => $insurAct);
say $m.made;
输出是:
===SORRY!=== Error while compiling /home/test.pl
Undeclared routine:
reformatDOB used at line 41
非常感谢你的帮助!!!
答案 0 :(得分:5)
您尝试调用不存在的子例程reformatDOB
,而不是方法。
与Java相比,Perl6不允许您省略调用,即方法调用必须写为
self.reformatDOB($DOB)
此外,还有像
这样的简写形式$.reformatDOB($DOB) # same as $(self.reformatDOB($DOB))
@.reformatDOB($DOB) # same as @(self.reformatDOB($DOB))
...
另外在返回值上加上上下文。
答案 1 :(得分:3)
另外:为什么重新发明轮子? Perl 6有Text :: CSV:
使用以下任一方式安装:
panda install Text::CSV
或:
zef install Text::CSV
答案 2 :(得分:1)
你是对的,因为没有调用指定捕获名称的action方法。相反,它将根据匹配的事物的名称调用方法。即将调用aField。
您可以通过TOP操作方法手动调用self.ptDOB($anInsurLine<ptDOB>)
。