Hi all,
Many thanks for your help - the algorithm is now working fine. For the moment I haven't tried implementing it as a function, but I might in the future. I'm attaching the working version at the end, for reference to others that want to do the same thing. And to clarify, the luhn algorithm is calculated on the 10 digit version of the swedish personal identification number, without the leading two digits indicating century ("19","20").
I've got a supplementary questions as well: I've now redefined preQ2 to be a string variable, but the RANGE check still works. How is this possible?
again, thank you very much for your help!
best,
Gustaf
------------Working version of the luhn algoritm as applied to swedish PID's --------------------
preQ2
RANGE 199201010001 201801010001 DEFINE pnr1 # global DEFINE pnr2 # global DEFINE pnr3 # global DEFINE pnr4 # global DEFINE pnr5 # global DEFINE pnr6 # global DEFINE pnr7 # global DEFINE pnr8 # global DEFINE pnr9 # global DEFINE pnr10 # global DEFINE pnr11 # global DEFINE pnr12 # global DEFINE checkSum ## global pnr1=INTEGER(copy(preQ2,1,1)) pnr2=INTEGER(copy(preQ2,2,1)) pnr3=INTEGER(copy(preQ2,3,1)) pnr4=INTEGER(copy(preQ2,4,1)) pnr5=INTEGER(copy(preQ2,5,1)) pnr6=INTEGER(copy(preQ2,6,1)) pnr7=INTEGER(copy(preQ2,7,1)) pnr8=INTEGER(copy(preQ2,8,1)) pnr9=INTEGER(copy(preQ2,9,1)) pnr10=INTEGER(copy(preQ2,10,1)) pnr11=INTEGER(copy(preQ2,11,1)) pnr12=INTEGER(copy(preQ2,12,1))
IF (pnr3>4) THEN pnr3=pnr3*2-9 ELSE pnr3=pnr3*2 ENDIF IF (pnr5>4) THEN pnr5=pnr5*2-9 ELSE pnr5=pnr5*2 ENDIF
IF (pnr7>4) THEN pnr7=pnr7*2-9 ELSE pnr7=pnr7*2 ENDIF
IF (pnr9>4) THEN pnr9=pnr9*2-9 ELSE pnr9=pnr9*2 ENDIF
IF (pnr11>4) THEN pnr11=pnr11*2-9 ELSE pnr11=pnr11*2 ENDIF checkSum=pnr3+pnr4+pnr5+pnr6+pnr7+pnr8+pnr9+pnr10+pnr11 IF (checkSum=10*int(checkSum)) THEN checkSum=checkSum/10 ELSE checkSum=10*(TRUNC(checkSum/10)+1)-checkSum ENDIF IF (pnr12<>checkSum) THEN HELP "felaktigt PersonNummer" TYPE=ERROR GOTO preQ2 ENDIF