in reply to Building data structure from multi-row/column table
You could build your data structure in several different ways, but they'll all have tradeoffs. If you don't know how you're planning on accessing the data, you won't know which tradeoffs make sense. For example, you could structure it like:
my %data = { 'CityA'=>{ 'PopByAge/Gender'=>{ 'Ages 0-9'=>{ 'Male' => 220, 'Female' => 180 }, ... }, 'FamilyHouseholds'=>{ ... }, }, ... };
This structure would hold your data just fine. It would be a convenient structure if you know that you'll always have the city name before the table name, and table name before the row headers, etc., like so:
print "What city? Choices are:\n", join(", ", sort keys %data), "\n>"; chomp($city = <>); my $hr = $data{$city}; print "What table? Choices are:\n", join(", ", sort keys %{$hr->{$city}}), "\n>"; chomp($table = <>); $hr = $hr->{$table}; print "Which group? Choices are:\n", join(", ", sort keys %{$hr->{$table}), "\n>"; chomp($grp = <>); $hr = $hr->{$grp}; ...
But if you wanted to access by table name and *then* city, it's less convenient, as you have to trawl through your data to build the list:
# Each city may have different tables, so trawl through all the # cities to find available table names my %tables; for my $city (keys %data) { $tables{$_}{$city}=$data{$k}{$_} for keys $data{$k}; } print "Which table? Choices are:\n", join(", ", sort keys %tables), ">\n"; chomp($table=<>); print "Which city? Choices are:\n", join(", ", sort keys %{$tables{$table}}), ">\n"; chomp($city=<>); ...
You'd have similar compromises if you organized it like:
my %data = { 'PopByAge/Gender'=>{ 'Ages 0-9'=>{ 'Male'=>{ 'CityA'=>220, 'CityB'=>... } } }, ... };
At the cost of space, you could build it with multiple hierarchies:
my %data = { 'Cities'=>{ 'CityA'=>{ 'PopByAge/Gender'=>{ 'Ages 0-9'=>{'Male' = 220,'Female' = 180}, ... },... }, 'Tables'=>{ 'PopByAge/Gender'=>{ 'Ages 0-9'=>{ 'CityA'=>{'Make'=>220, 'Female'=>180}, ... ... };
But this gets unwieldy as you add different orders of questioning.
Organizing your data in a hierarchy makes one set of questions really easy to ask, and others a bit less so. If you need to answer questions with no set hierarchy, you could flatten the data something like:
my @data = [ { city=>'CityA', table=>'PopByAge/Gender', grpA=>'Age Group', grpB=>'Gender', A=>'0-9', B=>'Male', val=>220 +}, ... ];
This not quite as friendly to build, and not as easy to query, but for some situations it's a reasonable compromise. You could let the user select whether they want to search by city, table, etc.:
my @selected = ( @data ); while (1) { print "Search by city, table or group? (or D for done)"; chomp($key=<>); last if $key eq 'D'; my @choices = map { $_->{$key} } @data; print "Choices are: ", join(", ", sort @choices), "\n>"; } ... print report on selected data ...
So rather than choose a data structure right now, find out how you want to access your data, and see how you can arrange your data to simplify things for yourself. If the user always gave me the city and table name at the same time, I'd choose a hierarchy because I personally find it easy. If the data were complex, and I wanted to keep it around, I'd use a database.
Eh, I've run on too long again. I guess I'll stop here... (Note: no code in this node is syntax checked or tested. No animals were harmed during filming. Do not use unless prescribed by your doctor. Talk to your dentist.)
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Building data structure from multi-row/column table
by GotToBTru (Prior) on Apr 03, 2015 at 19:00 UTC | |
by roboticus (Chancellor) on Apr 03, 2015 at 19:05 UTC |