Playfair cipher: Difference between revisions

m
→‎{{header|REXX}}: performed some subroutine statement reductions. -- ~~~~
m (→‎{{header|REXX}}: converted a subroutine to in-line. -- ~~~~)
m (→‎{{header|REXX}}: performed some subroutine statement reductions. -- ~~~~)
Line 360:
 
=={{header|REXX}}==
Quite a bit of the REXX code deals with error checking, accepting arguments, and displaying the options used.
<lang rexx>/*REXX program implements a PLAYFAIR cypher (encryption & decryption).*/
arg oldK noX _ . '(' oldX
Line 400 ⟶ 401:
@@: parse arg Xrow,Xcol; return @.Xrow.Xcol
err: say; say '***error!***' arg(1); say; exit 13
rowLR: ? rowL=posrow(argleft(__,1),grid); colL=_; XposrowR=row(?-right(__,1)//5+1); colR=_; return length(4+?__)%5
row: ?=pos(arg(1),grid); _=(?-1)//5+1; return (4+?)%5
/*──────────────────────────────────LR subroutine───────────────────────*/
LR: L=left(__,1); R=right(__,1); len=length(__)
rowL=row(L); colL=Xpos; rowR=row(R); colR=Xpos; return
/*──────────────────────────────────SCRUB subroutine────────────────────*/
scrub: procedure; arg stuff /* ARG capitalizes all arguments.*/
Line 417 ⟶ 416:
/*──────────────────────────────────.PLAYFAIR subroutine────────────────*/
.playfair: parse arg T,encrypt; i=-1; if encrypt then i=1; $=
do k=1 while encrypt; _=substr(T,k,1); if _==' ' then leave
if _==substr(T,k+1,1) then T=left(T,k) || 'X' || substr(T,k+1)
end /*k*/
do j=1 by 2 to length(T); __=strip(substr(T,j,2)); call LR
if len==1 | L==R then __=L__ || 'X'; call LR /*append an "X" character, rule 1*/
call LR
select
when rowL==rowR then __=@@(rowL, colL+i)@@(rowR, colR+i) /*rule 2*/
Line 428 ⟶ 426:
otherwise __=@@(rowL, colR )@@(rowR, colL) /*rule 4*/
end /*select*/
call LR
$=$ || __
end /*j*/