Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Hash-join/Perl/hash-join.pl
Normal file
30
Task/Hash-join/Perl/hash-join.pl
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
use Data::Dumper qw(Dumper);
|
||||
|
||||
sub hashJoin {
|
||||
my ($table1, $index1, $table2, $index2) = @_;
|
||||
my %h;
|
||||
# hash phase
|
||||
foreach my $s (@$table1) {
|
||||
push @{ $h{$s->[$index1]} }, $s;
|
||||
}
|
||||
# join phase
|
||||
map { my $r = $_;
|
||||
map [$_, $r], @{ $h{$r->[$index2]} }
|
||||
} @$table2;
|
||||
}
|
||||
|
||||
@table1 = ([27, "Jonah"],
|
||||
[18, "Alan"],
|
||||
[28, "Glory"],
|
||||
[18, "Popeye"],
|
||||
[28, "Alan"]);
|
||||
@table2 = (["Jonah", "Whales"],
|
||||
["Jonah", "Spiders"],
|
||||
["Alan", "Ghosts"],
|
||||
["Alan", "Zombies"],
|
||||
["Glory", "Buffy"]);
|
||||
|
||||
$Data::Dumper::Indent = 0;
|
||||
foreach my $row (hashJoin(\@table1, 1, \@table2, 0)) {
|
||||
print Dumper($row), "\n";
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue