I assume that you have the following:
- Current day's data as CSV
- Historical data as CSV or other input
In order to display the data, I assume that you're going to do something like this, Foreach Ship:
- Display current day's position as RED dot or similar
- Foreach historical data, display that position as BLUE dot or similar -- if you have it
IMHO the most simple thing to do would be:
- Prepare an HASH from the historical data -- if you have it -- like this:
- Keys are ships names
- Values are an ARRAY REF of the historical position, each unit like:
- {datetime => '...', position => '...'}
Once you've plotted today's data, all you need to do is go through all of today's data (foreach ship name) and:
- Push today's data to the historical hash of arrayrefs -- if you have no historical data, simply create the hash the first time
- save for later reference
Then, whenever you start the program up, you'll only have to:
- Load historical data
- see above for displaying
- see above for merging
- see above for saving
Now, for the hash part. The hash I have in mind would be like:
# today's data -- your starting point
my %SHIPS = (
SHIP1 => [
{datetime => '20080318-0730', latitude => '10.33', longitude => '-
+05.45'},
],
# other ships
);
You can then save that via Data::Dumper:
use Data::Dumper;
print Data::Dumper::Dumper([\%SHIPS],[*SHIPS]); # save to file, don't
+print
This specific invocation of Data::Dumper::Dumper will give you exactly what you have in the hash.
The cool thing is that at the subsequent start of the program all you have to do is to "do" the file, and Perl will get the contents of the %SHIPS hash you had at the previous run:
my %SHIPS;
do 'dumped_file';
As above, all you'd then have to do is display the data, merge it with the historical, and save again.
The "merge" (assuming you are reading through a CSV on <>) is something like:
while (<$CSV>) {
chomp;
# assume csv is SHIP1,DATETIME,LATITUDE,LONGITUDE.
# modify accordingly..
my ($shipname, $datetime, $latitude, $longitude) = split(/\,/$_);
my %today_details = (datetime => $datetime, latitude => $latitude, l
+ongitude => $longitude); # this is what will get pushed on the arrayr
+ef
my @arr = @{$SHIPS{$shipname} || []}; # get curr contents
push (@arr, \%today_details); # push today's data
$SHIPS{$shipname} = \@arr; # push back
}
# that's you done with the merge
Really, even if it does get VERY long... it's not like your PC can't handle a 10MB file after some time..
Or maybe you'd want to implement a "dump older than one month details".. you'll simply go through the hash, then through the arrayref, and discard all the entries which are older than a specified time.. Exercise left to the reader ;)
Hope this has helped :)