in reply to Hierarchial data structures
The question that keeps niggling me is: Why do you want an array at that point?
Arrays are indexed numerically, and 'year' is not numeric. And if 'year' is a placeholder for 1999, 2000, 2001 etc., then I still don't understand why you want an array. Array's start from 0 (ignoring $[ which is totally impractical), which unless your application needs to deal with antiquity, means that you'll be reserving (a relatively small amount) of space for almost 2000 entries in each 'date' array that you will never use.
Remember, that's one array for every 'galaxy' * 'solar system' * 'planet'; depending upon the size of your universe, that adds up to a lot of dark matter wasted space :)
The advantages of arrays over hashes for numeric data are:
If you are only using indexes 2000 .. 2020 of 0 .. 2020, then you will pretty much completely negate the latter, and the former is unlikely to make any appreciable difference given that you are already doing 5 other levels of hash lookup. Stick with a HoHoH(oH...)--it will make your life much simpler. You might look at YAML for your multi-dimensional configuration data. It seems to be the simplest/clearest format for manually maintained data.
Finally, for contrast with Zaxo's advice--for the purpose of giving the ammunition to make up your own mind--compare iterating all the latitudes and longitudes for a given year and incrementing the number of occurances:
Using a HoH:
my $thisYear = 2005; my $yearRef = $galaxy{ $sSystem }{ $planet }{ $thisYear }; for my $year ( keys %{ $yearRef } ) { for my $lat ( keys %{ $yearRef->{ $year } } ) { for my $long ( keys %{ $yearRef->{ $year }{ $lat } } ) { printf "$sSystem.$planet.$thisYear.$lat.$long:%s\n", $yearRef->{ $year }{ $lat }{ $long }; $yearRef->{ $year }{ $lat }{ $long }++; } } }
Using OO:
my $thisYear = 2005; my $yearIter = $galaxy->getYearIerator( $sSystem, $planet, $thisYear ) +; while( my $year = $yearIter->() ) { my $latIter = $year->getLatitudeIter(); while( my $lat = $latIter->() ) { my $longIter = $lat->getLongIter(); while( my $long = $longIter->() ) { printf "$sSystem.$planet.$thisYear.%s.%s.%s\n", $year->getYear(), $lat->getLat(), $long->getLong(), $long->getOccurances(); $long->setOccurances( $long->getOccurances() + 1 ); } } }
Your choice ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Hierarchial data structures
by Zaxo (Archbishop) on May 24, 2006 at 16:34 UTC | |
by BrowserUk (Patriarch) on May 24, 2006 at 16:52 UTC |