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

Hi, I'm trying to write a subroutine that:
1- opens a file to
overwrite a single variable in an array.
(of course it should lock the file too).

# example:
$flock = "y";
$RECORD = "data.db";
open (DAT,">$RECORD/$form{'User'}");

if ($flock eq "y") {
flock DAT, 2;
}

2- reads the file and go to a ceratin array
based on the input from a FORM (NAME="USER")

# example of array format
USER:FNAME:LNAME:EMAIL:COUNT

3- overwrites the last variable ($COUNT)
by subtracting one from the initial value
($COUNT--) only if it's greater or equal to one
($COUNT>=1) but if it's equal zero($COUNT=0)
it should print to an html document the variable value =0.
4-after ovewriting ($COUNT) it prints
the new value to an html document.

#example
print "count is now equal to $COUNT\n";

Please, Can anyone help me put this sub together?!!
  • Comment on How to overwrite one variable in an array?

Replies are listed 'Best First'.
Re: How to overwrite one variable in an array?
by VSarkiss (Monsignor) on Jun 28, 2001 at 01:24 UTC
    I'm not exactly clear what you're trying to do, but here are some general comments that I hope will point you in the right direction.

    The statement: open (DAT,">$RECORD/$form{'User'}"); will overwrite a file in the directory data.db, whose name is the value input by the user1, but I don't think that's what you intended. My guess is you want to open the file data.db, then find the record corresponding to the input value.

    If so, and you're trying to use a flat file, consider using fixed-size records. That way, you can open the file and seek to particular records either by searching sequentially or by saving the offsets of particular records.

    But the operations you're describing (find a record, change its value, write it back) are much more easily accomplished with a relational database system. Otherwise you'll be re-creating index-sequential systems that were mostly abandoned years ago (for many reasons, inflexibility being one of the primary ones). Then your sub may simply reduce to two SQL statements: fetch the data, update the data.

    If you don't have a database already, MySQL is an easy place to start. You can find many documents and examples for setting it up.

    HTH

    1BTW, creating files whose names come from user input is a bad practice in and of itself....

Re: How to overwrite one variable in an array?
by bikeNomad (Priest) on Jun 28, 2001 at 02:28 UTC
    You might want to look at DBD::CSV, which allows handling delimited files as if they were real databases.
Re: How to overwrite one variable in an array?
by mexnix (Pilgrim) on Jun 28, 2001 at 01:17 UTC
    I was actually preparing myself to ask a very similar question, although mine is geared in a different direction. The only way I can think to do this is to suck all though lines into you're array, then over the entire database (if you have more then one line of data, this). I was also hoping there was another way, becuase I have a plain-text database that comma-separated.

    I tried the search yielding meager results that were good, but not actually answering my question. Does anyone else have any ideas?

    __________________________________________________
    %mexnix = (email = > "mexnix@hotmail.com", website => "http://mexnix.perlmonk.org");