Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Another flatfile thingy

by tye (Sage)
on Aug 04, 2000 at 19:18 UTC ( [id://26204]=note: print w/replies, xml ) Need Help??


in reply to Another flatfile thingy

I'd put the records in an array of hashes ($data) then have a hash of hashes of arrays ($index) that returns record numbers (nah, make that references to the record hashes). Then let the user select which field(s) they want the index computed for. Then the user could do things like:

$record= $index{$keyName}{$keyValue}[0]; $fieldValue= $record->{fieldName}; $fieldValue= $data[$recordNumber]{$fieldName};
for example:
( $data, $index )= OpenFF( "flatfile.txt", qw(ID AGE) ); print "People ages 55 are:\n"; for( @{ $index{AGE}{55} } ) { print "\t",$_->{NAME},"\n"; }

The main reason I thought of this is because I thought your routine would be nice to use even when the first field isn't an ID number that is unique.

And, yes, I'd make the interface OO so you could have a method for setting the delimiter and for adding things to the index after the fact. Then if you have multiple files that use the same delimiter and ID fields, you could reuse the configuration of one for loading another via my $other= $one->new("other.txt");. Then have the object also be a reference to a tied hash where $obj->{}->[$N] gets a ref to record number $N and $obj->{keyFieldName}{fieldValue}[$I] gets a ref to the $I'th record having a matching key value. Also, each record should keep its record number in the field named "".

require File::Fields; my $staff= File::Fields->new(); $staff->Delimiter('\s+'); $staff->IndexOn( "ID" ); $staff->Open( "main.txt" ); my $students= $staff->new( "student.txt" ); my $TAs= $staff->new( "ta.txt" ); my $classes= File::Fields->new( "classes.txt", { Delimiter=>'\s*,\s*', IndexOn=>[qw(COURSE TEACH ROOM) } ); my $enroll= File::Fields->new( "enroll.txt", { IndexOn=>[qw(STUDENT COURCE)] } ); $staff->AddIndex( qw(PHONE OFFICE) ); $TAs->AddIndex( "ADVISOR" ); $staff->AddField("TOTALSTUDENTS",0); for my $teach ( @{ $staff->{} } ) { for my $class ( @{ $classes->{TEACH}{$teach} } ) { $teach->{TOTALSTUDENTS} += @{ $enroll->{COURSE}{$class} }; } } $staff->Write( "staff2.txt" );

Making it OO is pretty easy and once you do that, adding all of the features I mentioned is pretty easy and can be done incrementally. Making an object that is also a tied reference to a hash is confusing at first, so let me know if you'd like help there.

This probably sounds like a lot of work but I don't think that it would be. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://26204]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-25 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found