[Chicago-talk] creating relational keys
Jay Strauss
me at heyjay.com
Thu Mar 1 12:01:13 PST 2012
This seems to work:
This seems to work:
> perl create_hier.pl small.txt 11 10 12
use strict;
use Data::Dumper;
my %h;
$h{ROOT} = {};
my @i = splice @ARGV, 1, $#ARGV; # remove the indexes from command line
while (<>) {
my @f = split(/\|/);
my $p = $h{ROOT};
foreach my $i (@i) {
my $key = $f[$i];
if (! exists $p->{$key}) {
$p->{$key} = {};
}
$p = $p->{$key};
}
}
print Dumper %h;
my $counter = 0;
unwrap($counter, \%h);
sub unwrap {
my ($parent, $hash) = @_;
foreach my $key (keys %$hash) {
$counter++;
print join("\t", $counter, $parent, $key), "\n";
unwrap( $counter, $hash->{$key});
}
}
Giving me output like (I shortened it due to the size):
$VAR1 = 'ROOT';
$VAR2 = {
'LA' => {
'WALKER' => {
'70785' => {}
},
'BOUTTE' => {
'70039' => {}
},
'RAYNE' => {
'70578' => {}
},
'BATON ROUGE' => {
'70805' => {}
}
},
....
'NV' => {
'LAS VEGAS' => {
'89149' => {},
'89104' => {}
}
},
'OR' => {
'WARRENTON' => {
'97146' => {}
},
'SALEM' => {
'97305' => {},
'97302' => {},
'97301' => {}
}
}
};
1 0 ROOT
2 1 LA
3 2 WALKER
4 3 70785
5 2 BOUTTE
6 5 70039
7 2 RAYNE
8 7 70578
9 2 BATON ROUGE
10 9 70805
...
219 1 NV
220 219 LAS VEGAS
221 220 89149
222 220 89104
223 1 OR
224 223 WARRENTON
225 224 97146
226 223 SALEM
227 226 97305
228 226 97302
229 226 97301
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20120301/31e03421/attachment.html>
More information about the Chicago-talk
mailing list