Hi,
I'm trying to build a hash of arrays that could handle a (recursive relationship) between two data sets (USERS & LOCATION).
I'm pretty much lost right now.
I have two flat files: USERS & DATA
I want to create a hash of arrays and then output the data.
Based on the code and data I have below, my hash has the following structure:
Feedback on my code is also welcomed.
instead of
I'm trying to build a hash of arrays that could handle a (recursive relationship) between two data sets (USERS & LOCATION).
I'm pretty much lost right now.
I have two flat files: USERS & DATA
I want to create a hash of arrays and then output the data.
Based on the code and data I have below, my hash has the following structure:
Feedback on my code is also welcomed.
Code:
Hash
{12}{100} => [Bobby]
{14}{110} => [Ted]
{16}{120} => [Carey]
instead of
Code:
Hash
{12}{}{100} => [Bobby]
{12}{14}{110} => [Ted]
{12}{16}{120} => [Carey]
Code:
# Location of elements in Array (Users)
$USER_ID = 0;
$USER_PARENT_ID = 2;
$USER_NAME = 1;
# Location of elements in Array (Location)
$LOC_ID = 0;
$LOC_NAME = 1;
# Open File (LOCATIONS)
open(LOCATION,"Loc.txt") || die("Error");
my $fieldNames = <LOCATION>;
@Locs = map {chomp; [split /\|/, $_]} <LOCATION>;
close(LOCATION);
# Create Hash for Locations
my %Locations = map {$_->[$LOC_ID] => $_} @Locs;
# Open File
open(USERS,"Users.txt") || die("Error");
my $fieldNames = <USERS>;
@Users = <USERS>;
close(USERS);
# Populate Hash
foreach my $User (@Users) {
chomp $User;
my @fieldUser = split(/\|/,$User);
$userList{$fieldUser[$USER_PARENT_ID]}{$fieldUser[$USER_ID]} = [ @fieldUser ];
}
Code:
__USERS__ (Users.txt)
ID | USERNAME | LOC_ID
100 | Bobby | 12
110 | Ted | 14
120 | Carey | 16
Code:
__LOCATION__ (Loc.txt)
ID | NAME | LEVEL | PARENT
10 | US | 1 | 0
12 | Oregon | 2 | 10
14 | Seattle | 3 | 12
16 | Ostig | 3 | 12