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

I am using a flatfile database and i need to make certain parts of this database return back to 0 at a certain time. For an example say this is a just one of the multiple .txt files:
Grant
9
8
7

So to access this is I would open it using the file handle PERSON and then get the information into an array like so:
@cinfo = <PERSON>;
this would make $cinfo[0] = Grant and $cinfo1 = 9.

Now I need to make $cinfo1 = 0 in ALL of the .txt files in that directory. Any suggestions??

P.S. - As you may of noticed from the message i am relatively new to the world of perl and so any help would be greatly appriciated.

Replies are listed 'Best First'.
Re: making scalers back to 0....
by eg (Friar) on Dec 30, 2000 at 05:18 UTC

    If I understand correctly, you have a database of flat files -- that is, each text file is a single record -- and you want to periodically set a field (in this case, the second one) in each record to 0.

    If that's the case, something like

    cd [data directory]; for i in *.txt; do perl -pi.bak -e 's/.*/0/ if $. + == 2' $i; done

    in your crontab will work. ($. is the input line number, see perlvar.) This is imperfect, though, because of the possibility that two programs might both be trying to change the same text file simultaneously, but it might be Good Enough. Better would be to lock (see flock) each file before writing to it.

    Note that the general practice is to collect all records of them same type into a single file, rather than have them spread out in individual files, and have one of the fields be a unique identifier (the "primary key"). One advantage to this is that is makes searching through your database (relatively) fast and simple, since you only need to open one file.

    p.s. rather than posting the same question multiple times, it's better to a) create an actual user so you can edit your posts and b) read the Site How To.

Re: making scalers back to 0....
by elusion (Curate) on Dec 30, 2000 at 04:49 UTC
    This is how I would do it, but there could very possibly be a better way to do it.
    @temp_array = <PERSON>; #assign the information to a temporary array push @cinfo, shift @temp_array; #add the first element to @cinfo push @cinfo, '0', @temp_array; #add 0 & the rest to @cinfo

    - p u n k k i d
    "Reality is merely an illusion, albeit a very persistent one." -Albert Einstein

      easier to use the splice command. Good example here.