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

Greetings, I'm really, really stuck. I have a text file database, I want to be able assign a variable to each entry in the database and possibly create a new object for each entry on a new line (I hope this doesn't sound too vague). I know I can do, for example
open(FILE, $file) or die "Cannot open $file: $!\n"; while(<FILE>) { chomp;($creg, $cname, $attribs) = split(/,/); } close(FILE);
But how would I go about creating a new object for each entry (a new entry is on a new line), for example, my text file database is in the format:
UNIQUE ID, NAME, ATTRIBS UNIQUE ID, NAME, ATTRIBS
etc. I've been racking my brain to come up with something for days of google'ing, but I've ran out of ideas.. Any help will be lifesaving, litterally! I don't have much time left at all.. :( maybe something like
foreach $line(@file) { chomp;($creg, $cname, $attribs) = split(/,/); $object = $blah->new(); $object->creg($creg); ... }
I don't know :( Thank you for your time kind people, Shibby

Replies are listed 'Best First'.
Re: text file database - Objects?
by svsingh (Priest) on May 09, 2003 at 07:02 UTC
    I have a program that does something similar. Basically, I have a list of movies in a text file. I read each line and then parse the information into an anonymous hash. Then I stick a reference to the hash in a list. With the list, I can access every bit of data about every movie.

    Here's a simplified snippet of the read and store code.

    my @movieList = (); my $index = 0; open(DATA, $dataFile); while (<DATA>) { chomp; if (/^\s+$/) { next; } # skip blank lines in datafile my @data = split("\t"); my $title = &getString( shift(@data) ); my $year = &getString( shift(@data) ); my $movieRec = { 'index' => $index, 'title' => $title, 'year' => $year, }; push(@movieList, $movieRec); $index++; } # while close(DATA);

    I hope this gives you some ideas.

      Minor correction: That's not an anonymous hash. Sorry about that.
        Hello again,

        Sorry for my delayed reply, I've been very busy.. Thank you for your replies, they gave me ideas and I have solved my problem!
        Pseudo code

        for($i = 0; $i < scalar($#file +1); $i++) { foreach ($file[$i]) { # <-- this is what solves it! chomp; ($stuff, $more_stuff) = split(/\|/); # ... } }

        I didn't really need to create a new object for each data entry.. although it would have been a nice experiment, I found an easier way of doing what I wanted.
        Thanks very much for your support, I appreciate it.

        ~Shibby

Re: text file database - Objects?
by djantzen (Priest) on May 09, 2003 at 03:11 UTC

    I think you're on the right track, although you might try passing those arguments into the constructor rather than setting them after object creation. Also, if you assign $_ to an actual variable, you ought to use the var rather than rely on the implicit arguments to chomp and split:

    my @objects; foreach my $line (@file) { chomp($line); my ($creg, $cname, $attribs) = split(',', $line); my $object = Blah->new($creg, $cname, $attribs); push(@objects, $object); }
    Alternatively you might store the objects in a hash where the unique ID is the key.
    "The dead do not recognize context" -- Kai, Lexx
Re: text file database - Objects?
by nite_man (Deacon) on May 09, 2003 at 06:43 UTC
    Use Perl patterns ( this acrticle - Patterns in Perl will be useful for understanding of advantages of this method). For more detailes look through Perl Design Patterns TinyWiki
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);