RosettaCodeData/Task/Hash-join/Deja-Vu/hash-join.djv

20 lines
585 B
Plaintext

hashJoin table1 index1 table2 index2:
local :h {}
# hash phase
for s in table1:
local :key s! index1
if not has h key:
set-to h key []
push-to h! key s
# join phase
[]
for r in table2:
for s in copy h! r! index2:
push-through swap [ s r ]
local :table1 [ [ 27 "Jonah" ] [ 18 "Alan" ] [ 28 "Glory" ] [ 18 "Popeye" ] [ 28 "Alan" ] ]
local :table2 [ [ "Jonah" "Whales" ] [ "Jonah" "Spiders" ] [ "Alan" "Ghosts" ] [ "Alan" "Zombies" ] [ "Glory" "Buffy" ] ]
for row in hashJoin table1 1 table2 0:
!. row