Skip to content

Instantly share code, notes, and snippets.

@rsinger
Created March 8, 2010 15:53
Show Gist options
  • Save rsinger/325273 to your computer and use it in GitHub Desktop.
Save rsinger/325273 to your computer and use it in GitHub Desktop.
use strict;
use JSON::XS;
use Data::Dumper;
my $file = '/Volumes/External 7/shared/open-library/edition-2009-09-11.txt';
open(INFILE, $file) || die "Can't open '$file': $!";
my $i = 0;
my %elements;
sub amerge($$) {
my $h = shift;
my $otherh = shift;
foreach my $k (keys %$otherh) {
$h->{$k} = 1;
}
}
while (<INFILE>) {
my ($id, $type, $jsonstring) = split("\t");
my $j = decode_json($jsonstring);
$elements{$type} ||= {};
while (my ($key, $val) = each(%$j)) {
$elements{$key} ||= {};
$elements{$type}{$key} = 1;
my $vtype = ref($val);
if ($vtype eq 'ARRAY') {
next unless (ref($val->[0]) eq 'HASH'); # Somethign weird; ignore
foreach my $child (@$val) {
amerge $elements{$key}, $child;
}
} elsif ($vtype eq 'HASH') {
amerge $elements{$key}, $val;
}
}
}
close INFILE;
# Let's make it a little prettier
foreach my $k (keys %elements) {
$elements{$k} = [keys %{$elements{$k}}];
}
print Dumper(\%elements);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment