Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Flat File Comment

by PilotinControl (Pilgrim)
on Dec 09, 2013 at 22:45 UTC ( [id://1066348]=perlquestion: print w/replies, xml ) Need Help??

PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good Evening Monks. I thank all of your for your insight over the past few months and I've supported the Monestary by purchasing several items listed on Cafe Press.

My question is rather simple: I have several flat file databases and I would like to select a string which I already know how to do and that string is sent to a new file where strings from other files are merged to make one new string. For security purposes I would like to make it so the data from each file is unavailable.

File 1 Years

1999
2000
2001

File 2 Makes

Ford
Chevrolet
KIA

File 3 Models

Sorento
Cavalier
Expedition

Temp File would look like this:

2001 Ford Expedition

What I need to happen is some how commenting out the selected item from each file from being able to be selected again and once the temp file is deleted the comments would be removed. Anyone have an idea on how to accomplish this? I do not need code to select the strings or code to create a temp file with all the items listed that is already been coded. Just need direction to make the items unable to be selected. Another thought was removing the items from each file to the temp file then when the command is issued to delete the temp file the items would be placed back into their originating files? Thoughts? Thanks!

Replies are listed 'Best First'.
Re: Flat File Comment
by Kenosis (Priest) on Dec 09, 2013 at 23:19 UTC

    You don't mention the size of your files. If not overwhelming, consider creating a hash of hashes (HoH) from each file's contents. When you've used one of the Makes (for example), remove it by deleting it from the hash. For example:

    use strict; use warnings; my %hash; while (<DATA>) { chomp; $hash{Makes}{$_} = undef; } print 'Before:', "\n"; print "$_\n" for keys %{ $hash{Makes} }; my $item = getCategoryItem( 'Makes', \%hash ); print "\n", 'Selected:', "\n", $item, "\n\n"; print 'After:', "\n"; print "$_\n" for keys %{ $hash{Makes} }; sub getCategoryItem { my ( $category, $hashRef ) = @_; my @items = keys %{ $hashRef->{$category} } or return undef; # undef is returned if no items my $item = $items[ rand $#items + 1 ]; delete $hashRef->{$category}{$item}; return $item; } __DATA__ Ford Chevrolet KIA

    Sample output:

    Before: KIA Chevrolet Ford Selected: KIA After: Chevrolet Ford

    Hope this helps!

Re: Flat File Comment
by ig (Vicar) on Dec 10, 2013 at 04:20 UTC

    The usual way to control access to files is to set the ownership and permissions on the files and the directories that contain them so that only authorized users are able to access them.

    There are database systems with well designed and well tested security and access control capabilities, indexes and constraints - all you need to solve the problems you have described. If security were a concern, I would use proven systems rather than design and build my own.

      Hello IG,
      It's not so much a security issue as it is the following: A master file contains all the records, once an item from that master file has been selected it is "copied" to a new "temp" file for use in the program. Once the temp file has out lived its use the items in the master file are able to be selected again. The issue is how do I make the items selected from the master file remain un-selectable until the temp file has been removed? Also the files in use are only a few KB each. Thanks for the ideas and suggestions.

        In that case, rather than editing the master files, I would probably scan the temp file and index its contents, then scan the master file and exclude anything found in the index. If the temp file doesn't exist, then the index will be empty and nothing will be excluded.

Re: Flat File Comment
by kcott (Archbishop) on Dec 10, 2013 at 15:56 UTC

    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

      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.

      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
        $ pm_year_make_model.pl 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
        $ pm_year_make_model.pl 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1066348]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found