Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Changing records

by arrow (Friar)
on Apr 03, 2003 at 03:07 UTC ( [id://247657]=perlquestion: print w/replies, xml ) Need Help??

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

I'm making this administrative website and one of its functions is editing the names of members. The thing is, I have text files containing records of who worked when and for how long, which is separate from my list of members. For example:

Member List

J. Smith
D. Smith
M. Smith
etc...

Records

6/11/2002+J. Smith,D. Smith,M. Smith+2
Format: Date+Members+Hours

If I want to change J. Smith to T. Smith, I can do that with the member list, but not with the records file, because they are in different files. Two solutions for this problem are: 1. Have the script search for all occurances of the name (in both files), then change it to the new one. 2. Assign numbers to the names, and in the records file the members will look like-1,5,7,8-corresponding to the members in the member list, thereby allowing me to concentrate solely on editing the member list. Any better suggestions? Which idea do you like best? Thanks in advance for any assistance.

Just Another Perl Wannabe

Replies are listed 'Best First'.
Re: Changing records
by graff (Chancellor) on Apr 03, 2003 at 03:25 UTC
    Your latter idea, assigning ID numbers to people, having "number,name" in one file and "date,number(s),hours" in the other file, is definitely the way to go. This is called normalizing a database -- basically, don't store a piece of "mutable" data in more than one place.

    For that matter, don't store more than one name/person-number in a single "hours" record: i.e., it should always be just "date,person_number,hours", one person mentioned per row.

    As for better suggestions, try actually using a relational database (e.g. mysql), where this sort of thing is quite natural and easy. (But that's not essential -- for something on this scale, you could just as well use perl hashes to provide the same functionality without breaking a sweat.)

Re: Changing records
by kschwab (Vicar) on Apr 03, 2003 at 03:29 UTC
    I would go with #2. In technical terms, this is referred to as database normalization. See this blurb from devshed that explains the benefits.

    I'd also recommend using a more easily updated datastore like a tied hash. That will make it easier for you to manipulate records without rewriting the whole file.

    Last thought...you may want to look into some sort of locking mechanism to handle multiple concurrent updates.

Re: Changing records
by pg (Canon) on Apr 03, 2003 at 03:24 UTC
    Why not database? This kind of flat file system is really difficult to maintain, change file format in the future would be a big hussle.

    Using a database system, you would get the searching capability for free.

Re: Changing records
by DrManhattan (Chaplain) on Apr 03, 2003 at 03:41 UTC

    Any reason not to just use the records file? It seems like the members file is redundant.

    -Matt

      I use the members list for other pages, like the View Hours page and the Add Hours page. This keeps me from hardcoding names into the scripts.

      Just Another Perl Wannabe
        Could you adapt the other scripts to use the records file instead of the members file? That way you'd only have to deal with one data source.

        -Matt

Re: Changing records
by arrow (Friar) on Apr 03, 2003 at 03:51 UTC
    Yeah, I know, databases are the "bomb", but I don't have the funds to pay for a host that has databases. I'm working strictly with CGI on the internet, so installing mySQL or other is out of the question. I guess flat files are the only thing for me :-{...

    Just Another Perl Wannabe
      >I'm working strictly with CGI on the internet

      There are Perl modules which will make it easier for you to treat flat text files as if they were databases, including doing SQL-like queries on them. You could check out Tie::File and Tie::CSV_File and DBD::CSV and there's another one I can't remember.
      --

      “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
      M-J D

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-18 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found