Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Flat File Comment

by kcott (Chancellor)
on Dec 10, 2013 at 15:56 UTC ( #1066454=note: print w/replies, xml ) Need Help??

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:

-- Ken

Replies are listed 'Best First'.
Re^2: Flat File Comment
by PilotinControl (Monk) on Dec 10, 2013 at 18:06 UTC

    Hello Ken,
    That is exactly what I was trying to accomplish! Two thumbs up! Local car dealer will be very satisfied now. Thanks again Ken and all the monks for their input.

      We're all (I'm sure) very happy that you're so happy... so please consult your client on how best to have ken paid...
            unless, that is, you've already made arragements to pay him yourself.

Re^2: Flat File Comment
by PilotinControl (Monk) on Dec 17, 2013 at 22:45 UTC

    I've hit a snag and it seems if I have a field that states both Expedition XLT and Expedition when either one is selected one Expedition ends up getting a ##Expedition in the used file. The script is not recognizing via the

    chomp(my $selection = <STDIN>);

    as it is only taking the first part of the input and not the whole input as one. I believe I may have to use a while loop or use the m// operator correct? Thanks for the suggestions.

      I can't reproduce the problem with the code I posted. You haven't shown what code you're using: see "How do I post a question effectively?" for guidelines on what to post such that we can help you.

      Here's my test:

      $ cat pm_year_make_model_file_models.master Sorento Cavalier Expedition
      $ cat >> pm_year_make_model_file_models.master Expedition XLT
      $ rm pm_year_make_model_temp_file
      $ Available years: 1999 2000 2001 Select year: 1999 Available makes: Chevrolet Ford KIA Select make: Ford Available models: Cavalier Expedition Expedition XLT Sorento Select model: Expedition Temp file contents: 1999 Ford Expedition
      $ cat pm_year_make_model_file_models.used Sorento Cavalier #Expedition Expedition XLT
      $ Available years: 2000 2001 Select year: 2000 Available makes: Chevrolet KIA Select make: KIA Available models: Cavalier Expedition XLT Sorento Select model: Expedition XLT Temp file contents: 1999 Ford Expedition 2000 KIA Expedition XLT
      $ cat pm_year_make_model_file_models.used Sorento Cavalier #Expedition #Expedition XLT

      -- Ken

        The added code is below. Since the program is actually sharing the master files which look like this:

        Expedition:XLT:Power Locks:Power Doors

        The code added strips the ":" and then joins only the two fields needed for my side of the program. I am not originating the files another program is doing that.

        for (@models) { my @mfields = split /:/; $_ = join ' ' => $mfields[0], $mfields[1]; }

        When selected the program only recognizes the first field not the second when joined together creating the following output:

        Expedition XLT

        Thus not commenting out each Expedition

        FIXED!! Using the below code I have no issues splitting the fields and rejoining the fields using split and splice together. Thanks for the suggestions they have been most helpful.

        for (@models) { my @mfields = split /:/, $_; $_ = join ( ' ',splice( @mfields, 0, 2)); }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1066454]
[stevieb]: Finally, somehow, fried my first Raspberry Pi :) It was one I was using in a mock grow room with no protection, so I think humidity got to it.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2018-01-16 21:57 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (192 votes). Check out past polls.