Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Syntactic Confectionary Delight
 
PerlMonks

text file database - Objects?

by shibby (Initiate)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on May 09, 2003 at 02:28 UTC ( #256739=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on text file database - Objects?
Select or Download Code
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
[reply]
[d/l]
[select]
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);
    
[reply]
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.

[reply]
[d/l]
      Minor correction: That's not an anonymous hash. Sorry about that.
[reply]
        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

[reply]
[d/l]

Back to Seekers of Perl Wisdom


Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: perlquestion [id://256739]
Approved by djantzen
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others drinking their drinks and smoking their pipes about the Monastery: (8)
GrandFather
wfsp
atcroft
herveus
Eyck
biohisham
lamprecht
gnosti
As of 2009-11-21 09:06 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (729 votes), past polls