-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastructure.pl
executable file
·89 lines (69 loc) · 2.91 KB
/
datastructure.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
use JSON; # Uncomment this line if you have the JSON module
# To pretty print hash
$Data::Dumper::Indent = 1;
# Read data from STDIN
# STDIN must be comma separated data with the following columns:
# userid,parentid,childid,childname,idirectory
open(DATA,"-") or die "Can't open data: $!";
# Datastructure Hashes
my $hierarchy_pc; # Intermediary hash to store parent to child relationship
my $hierarchy; # Final hash to store parent to child relationship
#-------------------------------------------------------------------------------
# Step 1 of 3
#
# Read lines from STDIN
# STDIN must be comma separated data with the following columns:
# userid,parentid,childid,childname,idirectory
#
#-------------------------------------------------------------------------------
while(<DATA>) {
next if(/^\s*$/ | /^\s*#/ );
my( $userid, $parentId, $childId, $childName, $isdirectory ) = split(/,/, $_);
$isdirectory =~ s/\n//g;
# Determine if datastructure element is a folder or file
# this needs improvement
my $type = 'folder' if( $isdirectory =~ /TRUE|1/gi );
$type = 'file' if( $isdirectory =~ /FALSE|0/gi );
# Add name & type of datastructure element to hash for $parentId & $childId
$hierarchy_pc->{$parentId}->{'children'}->{$childId}->{'name'} = $childName;
$hierarchy_pc->{$parentId}->{'children'}->{$childId}->{'type'} = $type;
# For folders add children key
if( $type eq 'folder' ) {
$hierarchy_pc->{$parentId}->{'children'}->{$childId}->{'children'} = {};
}
}
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Step 2 of 3
#
# Determine if a child is also a parent; if so, add it to $hierarchy
#
#-------------------------------------------------------------------------------
foreach my $parentId ( keys %{$hierarchy_pc} ) {
foreach my $childId ( keys %{$hierarchy_pc->{$parentId}->{'children'}} ) {
# Copy $hierarchy_pc to $hierarchy
$hierarchy->{$parentId}->{'children'} = $hierarchy_pc->{$parentId}->{'children'};
# Is child $childId also a parent ?
if( exists $hierarchy_pc->{$childId} ) {
$hierarchy->{"$parentId"}->{'children'}->{"$childId"}->{'children'} = $hierarchy_pc->{"$childId"}->{'children'};
}
}
}
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Step 3 of 3
#
# Delete keys whose parent is not <HOME>. They are aliens.
#
#-------------------------------------------------------------------------------
foreach my $k ( keys %{$hierarchy} ) {
if( $k !~ /\<HOME\>/i ) {
delete $hierarchy->{"$k"};
}
}
#-------------------------------------------------------------------------------
print to_json( $hierarchy, { ascii => 1, pretty => 1 } );