2017-02-06 15 views
3

간단한 작업을 수행하기 위해 CSV 파일을 구문 분석하려고합니다. 성, ID 및 생일을 추출하고 생일 형식을 m/d/yyyy yyyymmdd.perl6 문법 작업 클래스 메서드가 상속되지 않은 것으로 보입니다. 캡처 된 것으로 보입니다.

(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 

는 당신의 도움을 주셔서 감사합니다!

답변

5

메서드가 아닌 reformatDOB이 아닌 기존 서브 루틴을 호출하려고합니다.

, 말, 자바, Perl6는 메서드 호출이 또한

self.reformatDOB($DOB) 

로 작성되어야한다, 즉 당신의 invocant를 생략 할 수 없습니다 대조적으로

,

같은 축약 형태도 있습니다
$.reformatDOB($DOB) # same as $(self.reformatDOB($DOB)) 
@.reformatDOB($DOB) # same as @(self.reformatDOB($DOB)) 
... 

반환 값에 컨텍스트를 추가로 지정합니다.

+0

고맙습니다, 크리스토프. 나는 그것이 단순해야만한다는 것을 안다. 흔히 발견하기 어려운 아주 작은 것들입니다! 감사. – lisprogtor

3

기타 : 왜 바퀴를 다시 만들까요? 텍스트 :: CSV 펄 6있다 :

https://github.com/Tux/CSV

중 하나를 사용하여 설치 :

panda install Text::CSV 

나 :

zef install Text::CSV 
+0

Elizabeth Mattijsen! 감사합니다. 나는이 패키지를 조사 할 것이다. – lisprogtor

1

당신이 점에서 권리의 이름에 대한 조치 방법입니다 명명 된 캡처가 호출되지 않습니다. 대신, 그것은 일치 된 것의 이름을 기반으로 메서드를 호출합니다. 나는. aField가 호출됩니다.

TOP 액션 메소드에서 self.ptDOB($anInsurLine<ptDOB>)을 수동으로 호출 할 수 있습니다.

+0

티모시모 감사합니다! 내 자신이 원하는 데이터를 만들기 위해 인수가 일치하는 메소드를 호출하면 필요한 퍼즐의 마지막 부분입니다. 감사 ! – lisprogtor