Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found