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. :)


In reply to Re: Another flatfile thingy by tye
in thread Another flatfile thingy by BBQ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.