Skip to content

Instantly share code, notes, and snippets.

@billdueber
Created March 8, 2010 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billdueber/325356 to your computer and use it in GitHub Desktop.
Save billdueber/325356 to your computer and use it in GitHub Desktop.
use strict;
use JSON;
use Data::Dumper;
my $file = 'gistfile1.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 = from_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