in reply to Flat File Comment
G'day PilotinControl,
As your files are very small, you could just make working copies where usage is flagged. Each time you find the temp file has been deleted, just recreate the working copies from the master files.
Here's a proof-of-concept script.
#!/usr/bin/env perl use strict; use warnings; use autodie; use File::Copy; use Tie::File; my $master_ext = '.master'; my $used_ext = '.used'; my $year_file_base = 'pm_year_make_model_file_years'; my $make_file_base = 'pm_year_make_model_file_makes'; my $model_file_base = 'pm_year_make_model_file_models'; my $temp_file = 'pm_year_make_model_temp_file'; my $used_years = "$year_file_base$used_ext"; my $used_makes = "$make_file_base$used_ext"; my $used_models = "$model_file_base$used_ext"; if (! -e $temp_file) { copy("$year_file_base$master_ext", $used_years); copy("$make_file_base$master_ext", $used_makes); copy("$model_file_base$master_ext", $used_models); } tie my @years, 'Tie::File', $used_years; tie my @makes, 'Tie::File', $used_makes; tie my @models, 'Tie::File', $used_models; my $year = select_item('year', \@years); my $make = select_item('make', \@makes); my $model = select_item('model', \@models); untie @years; untie @makes; untie @models; open my $fh, '>>', $temp_file; print $fh "$year $make $model\n"; close $fh; print "Temp file contents:\n"; system cat => $temp_file; sub select_item { my ($item, $records_ref) = @_; my %item_index_for = map { $records_ref->[$_] => $_ } grep { $records_ref->[$_] !~ /^#/ } 0 .. $#$records_ref; print "Available ${item}s:\n"; print "$_\n" for sort keys %item_index_for; print "Select $item: "; chomp(my $selection = <STDIN>); substr($records_ref->[$item_index_for{$selection}], 0, 0) = '#'; return $selection; }
Here's some sample runs:
Start with no temp file:
$ rm pm_year_make_model_temp_file
First run - all selections available:
$ pm_year_make_model.pl Available years: 1999 2000 2001 Select year: 2001 Available makes: Chevrolet Ford KIA Select make: Ford Available models: Cavalier Expedition Sorento Select model: Expedition Temp file contents: 2001 Ford Expedition
Second run - reduced selections available:
$ pm_year_make_model.pl Available years: 1999 2000 Select year: 1999 Available makes: Chevrolet KIA Select make: KIA Available models: Cavalier Sorento Select model: Cavalier Temp file contents: 2001 Ford Expedition 1999 KIA Cavalier
The temp file is deleted at some point:
$ rm pm_year_make_model_temp_file
All selections are available again:
$ pm_year_make_model.pl Available years: 1999 2000 2001 Select year: 1999 Available makes: Chevrolet Ford KIA Select make: Chevrolet Available models: Cavalier Expedition Sorento Select model: Sorento Temp file contents: 1999 Chevrolet Sorento
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Flat File Comment
by PilotinControl (Pilgrim) on Dec 10, 2013 at 18:06 UTC | |
by ww (Archbishop) on Dec 10, 2013 at 18:24 UTC | |
|
Re^2: Flat File Comment
by PilotinControl (Pilgrim) on Dec 17, 2013 at 22:45 UTC | |
by kcott (Archbishop) on Dec 18, 2013 at 13:01 UTC | |
by PilotinControl (Pilgrim) on Dec 18, 2013 at 14:53 UTC |