Soundex: Difference between revisions

1,647 bytes added ,  2 years ago
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
Line 3,789:
echo soundex("Ekzampul"), "\n"; // E251
?></lang>
 
=={{header|Picat}}==
{{trans|C#}}
<lang Picat>go =>
Names = split("Lloyd Woolcock Donnell Baragwanath Williams Ashcroft Ashcraft Euler
Ellery Gauss Ghosh Hilbert Heilbronn Knuth Kant Ladd Lukasiewicz Lissajous O'Hara"),
SoundexNames = split("L300 W422 D540 B625 W452 A261 A261 E460
E460 G200 G200 H416 H416 K530 K530 L300 L222 L222 O600"),
 
foreach({Name,Correct} in zip(Names, SoundexNames))
S = soundex(Name),
printf("%s: %s ",Name,S),
if S == Correct then
println("ok")
else
printf("not correct! Should be: %s\n", Correct)
end
end,
nl.
 
soundex("", _) = "" => true.
soundex(Word) = Soundex =>
SoundexAlphabet = "0123012#02245501262301#202",
Soundex = "",
LastC = '?',
foreach(Ch in Word.to_uppercase,
C = ord(Ch), C >= 0'A', C <= 0'Z',
Soundex.len < 4)
ThisC := SoundexAlphabet[C-0'A'+1],
Skip = false, % to handle '#'
if Soundex.len == 0 then
Soundex := Soundex ++ [Ch]
elseif ThisC == '#' then
Skip := true
elseif ThisC != '0', ThisC != LastC then
Soundex := Soundex ++ [ThisC]
end,
if Skip == false then
LastC := ThisC
end
end,
Soundex := Soundex.padRight(4,'0').
 
padRight(S,Len,PadChar) = S ++ [PadChar : _ in 1..Len-S.len].</lang>
 
{{out}}
<pre>Lloyd: L300 ok
Woolcock: W422 ok
Donnell: D540 ok
Baragwanath: B625 ok
Williams: W452 ok
Ashcroft: A261 ok
Ashcraft: A261 ok
Euler: E460 ok
Ellery: E460 ok
Gauss: G200 ok
Ghosh: G200 ok
Hilbert: H416 ok
Heilbronn: H416 ok
Knuth: K530 ok
Kant: K530 ok
Ladd: L300 ok
Lukasiewicz: L222 ok
Lissajous: L222 ok
O'Hara: O600 ok</pre>
 
=={{header|PicoLisp}}==
495

edits