Hash join: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: further simplify the code)
m (→‎{{header|Sidef}}: minor code simplifications)
Line 1,956: Line 1,956:
=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func hashJoin(table1, index1, table2, index2) {
<lang ruby>func hashJoin(table1, index1, table2, index2) {
var a = Arr.new;
var a = []
var h = Hash.new;
var h = Hash()


# hash phase
# hash phase
table1.each { |s|
table1.each { |s|
h{s[index1]} := [] -> append(s);
h{s[index1]} := [] << s
};
}


# join phase
# join phase
table2.each { |r|
table2.each { |r|
a += h{r[index2]}.map{[_,r]};
a += h{r[index2]}.map{[_,r]}
};
}


return a;
return a
}
}


Line 1,976: Line 1,976:
[28, "Glory"],
[28, "Glory"],
[18, "Popeye"],
[18, "Popeye"],
[28, "Alan"]];
[28, "Alan"]]


var t2 = [["Jonah", "Whales"],
var t2 = [["Jonah", "Whales"],
Line 1,982: Line 1,982:
["Alan", "Ghosts"],
["Alan", "Ghosts"],
["Alan", "Zombies"],
["Alan", "Zombies"],
["Glory", "Buffy"]];
["Glory", "Buffy"]]


hashJoin(t1, 1, t2, 0).each { .dump.say };</lang>
hashJoin(t1, 1, t2, 0).each { .say }</lang>
{{out}}
{{out}}
<pre>[[27, 'Jonah'], ['Jonah', 'Whales']]
<pre>[[27, 'Jonah'], ['Jonah', 'Whales']]