Hash join: Difference between revisions

41 bytes removed ,  10 years ago
→‎{{header|Tcl}}: Corrected problems
(Added zkl)
(→‎{{header|Tcl}}: Corrected problems)
Line 913:
 
=={{header|Tcl}}==
{{Incorrect|Tcl|This implementation misses the rows "18, Alan, Ghosts" and "18, Alan, Zombies" in the resulting relation.}}
Tcl uses hash tables to implement both its associative arrays and its dictionaries.
<lang tcl>package require Tcl 8.6
# Only for lmap, which can be replaced with foreach
 
proc joinTables {tableA indexAa tableB indexBb} {
# Optimisation: if the first table is longer, do in reverse order
if {[llength $tableB] < [llength $tableA]} {
return [lmap pair [joinTables $tableB $indexBb $tableA $indexAa] {
lreverse $pair
}]
Line 927 ⟶ 926:
 
foreach value $tableA {
setlappend hashmap([lindex $value $indexAa]) [lreplace $value $a $a]
#dict version# dict setlappend hashmap [lindex $value $indexAa] [lreplace $value $a $a]
}
lmapset value $tableBresult {}
set key [lindex $ foreach value $indexB]tableB {
set key [lindex $value $b]
if {![info existexists hashmap($key)]} continue
#dict version# if {![dict exists $hashmap $key]} continue
listforeach first $hashmap($key) $value{
#dict version# listforeach first [dict get $hashmap $key] $value
lappend result [list {*}$first $key {*}[lreplace $value $b $b]]
}
}
return $result
}
 
Line 952 ⟶ 955:
{{out}}
<pre>
{27 "Jonah"} {"Jonah" "Whales"}
{27 "Jonah"} {"Jonah" "Spiders"}
{2818 "Alan"} {"Alan" "Ghosts"}
{28 "Alan"} {"Alan" "Zombies"}Ghosts
18 Alan Zombies
{28 "Glory"} {"Glory" "Buffy"}
28 Alan Zombies
{28 "Glory"} {"Glory" "Buffy"}
</pre>
 
Anonymous user