Created
March 8, 2010 17:03
-
-
Save billdueber/325356 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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