Hash join: Difference between revisions

2,499 bytes added ,  1 month ago
Added FreeBASIC
mNo edit summary
(Added FreeBASIC)
 
(3 intermediate revisions by 3 users not shown)
Line 305:
{age:28, |name|:"Alan", |character|:"Alan", nemesis:"Ghosts"},
{age:28, |name|:"Alan", |character|:"Alan", nemesis:"Zombies"}}</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">hashJoin: function [t1, t2][
result: []
h: #[]
loop t1 's [
if not? key? h s\1 -> h\[s\1]: []
h\[s\1]: h\[s\1] ++ @[s]
]
loop t2 'r [
loop h\[r\0] 's [
'result ++ @[@[s r]]
]
]
return result
]
 
table1: [
[27 "Jonah"]
[18 "Alan"]
[28 "Glory"]
[18 "Popeye"]
[28 "Alan"]
]
 
table2: [
["Jonah" "Whales"]
["Jonah" "Spiders"]
["Alan" "Ghosts"]
["Alan" "Zombies"]
["Glory" "Buffy"]
]
 
loop hashJoin table1 table2 'row ->
print row</syntaxhighlight>
 
{{out}}
 
<pre>[27 Jonah] [Jonah Whales]
[27 Jonah] [Jonah Spiders]
[18 Alan] [Alan Ghosts]
[28 Alan] [Alan Ghosts]
[18 Alan] [Alan Zombies]
[28 Alan] [Alan Zombies]
[28 Glory] [Glory Buffy] </pre>
 
=={{header|AWK}}==
Line 965 ⟶ 1,010:
<syntaxhighlight lang="lisp">
(defun make-multi-map (rows)
(let ((map1multi-map '()nil))
(defun place_line (cl-loop for row) in rows do
(let* ((name (car row)))
(ifname-list (assoc name map1multi-map)))
(letif ((rcname-list (assoc name map1)))
(setcdrnconc (nthcdr (1- (length rcname-list)) rc-list) (list row)) )
(progn
(add-to-list 'map1 (list name row) 't)) ) )
(add-to-list 'multi-map (list name row) 't) ) ) ) )
(mapcar 'place_line rows)
map1 multi-map) )
 
(defun join-tables (table1 table2)
(let ((multi-map (make-multi-map table2))
(result-table '()))
(cl-loop for row in table1 do
(let ((multi-rc (assoc (cdr row) multi-map)))
for row in table1 do
(prognwhen multi-rc
(cl-loop for (let ((multi-rcline (associn (cdr row) multi-map))rc) do
(add-to-list 'result-table
(when multi-rc
(cl-looplist for(car row) (cdr row) (car multi-line in) (cdr multi-rcline)) do
(add-to-list 'result-table (list row multi-line) 't))))))
result-table))
 
 
(let ((table1 '((27 . "Jonah")
Line 998 ⟶ 1,042:
("Alan" . "Zombies")
("Glory" . "Buffy"))))
(message "%s" (join-tables table1 table2)) )
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Type Data1
value As Integer
key As String
End Type
 
Type Data2
key As String
value As String
End Type
 
Dim table1(5) As Data1
Dim table2(5) As Data2
 
table1(1).value = 27: table1(1).key = "Jonah"
table1(2).value = 18: table1(2).key = "Alan"
table1(3).value = 28: table1(3).key = "Glory"
table1(4).value = 18: table1(4).key = "Popeye"
table1(5).value = 28: table1(5).key = "Alan"
 
table2(1).key = "Jonah": table2(1).value = "Whales"
table2(2).key = "Jonah": table2(2).value = "Spiders"
table2(3).key = "Alan": table2(3).value = "Ghosts"
table2(4).key = "Alan": table2(4).value = "Zombies"
table2(5).key = "Glory": table2(5).value = "Buffy"
 
Print String(51, "-")
Print " Age | Name || Name | Nemesis"
Print String(51, "-")
 
For i As Integer = 1 To 5
For j As Integer = 1 To 5
If table1(i).key = table2(j).key Then
Print Using " ## | \ \ || \ \ | \ \"; table1(i).value; table1(i).key; table2(j).key; table2(j).value
End If
Next j
Next i
 
Sleep</syntaxhighlight>
{{out}}
<pre>---------------------------------------------------
Age | Name || Name | Nemesis
---------------------------------------------------
27 | Jonah || Jonah | Whales
27 | Jonah || Jonah | Spiders
18 | Alan || Alan | Ghosts
18 | Alan || Alan | Zombies
28 | Glory || Glory | Buffy
28 | Alan || Alan | Ghosts
28 | Alan || Alan | Zombies</pre>
 
=={{header|F_Sharp|F#}}==
Line 3,392 ⟶ 3,487:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
class A {
2,123

edits