|
PTools::SDF::IDX - Allow user-defined indices into PTools::SDF::SDF objects |
PTools::SDF::IDX - Allow user-defined indices into PTools::SDF::SDF objects
This document describes version 0.15, released October, 2004.
# Trivial example reads /etc/passwd, indexes on Uid,
# and prints the user information for c-shell users.
use PTools::SDF::IDX;
@pwFields = qw(uname passwd uid gid gecos home shell );
$pw = new PTools::SDF::IDX("/etc/passwd", undef, undef, @pwFields);
$idx = $pw->indexInit('uid', 'shell', '=~/csh$/');
foreach (sort keys %$idx) {
@idx = ('uid',$_);
print $pw->index(@idx, 'gecos'), "\n"
}
This perl library extends the PTools::SDF::SDF class to make it easy to create user defined indices. The SDF class loads a field delimited file into an array of associative arrays.
Until now, the only index into the SDF object was the record number. This meant that repeated serial reads of an SDF object were necessary to match on any other fields. Now any arbitrary field can be used to index the file.
Match criteria can be used when initializing the index to select a subset of the records to be indexed. This means that the value of the field indexed does not necessarily need to be unique for every record in the file. It does mean, however, that the field values must be unique with the selection criteria. Therefore, care may be needed when creating the match criteria.
None.
As a subclass of PTools::SDF::SDF this uses the constructor in the parent class. See the PTools::SDF::SDF manpage.
As an alternative to this, see the compoundInit method, below.
Examples:
The 'indexInit' method must be called once for each user-defined index before invoking the 'index' method using that index. Create an IDX object as you would any SDF object.
use PTools::SDF::IDX; $sdfObj = new PTools::SDF::IDX( $ProductFile );
This first example simply indexes the SDF object on the 'prodnbr' field of the file specified in the $ProductFile variable.
$sdfObj->indexInit('prodnbr');
In this next example, 'prodnbr' records are indexed only when the 'type' field does not equal the string ``Format''.
$sdfObj->indexInit($fieldname, 'type', 'ne "Format"');
This last example show a somewhat more complex set of matching criteria.
%matchCriteria = (
type => '!~ /Format|Display Only/',
prodnbr => 'lt "99999"',
MATCH_ORDER => 'type:prodnbr',
);
$sdfObj->indexInit($fieldname, %matchCriteria);
Since the match criteria are stored in an associative array no particular selection order is enforced. To impose an order of priority on a set of criteria, use the MATCH_ORDER key to specify in which order the matching will occur.
Example:
# Determine which type of index to initialize
$compound = 1;
$field = ($compound ? 'prodnbr&xyzzy' : 'prodnbr' );
if ($field =~ /&/) {
$sdfObj->compoundInit( $field );
} else {
$sdfObj->indexInit( $field );
}
See the last example in the B<index> method, below for the syntax used to access a compund index.
Examples:
$otherFieldValue = $sdfObj->index('prodnbr',$prodNbrValue, 'otherfield');
Think of the (IdxFieldName,IdxFieldValue) combination as an index or 'record number' that is passed to the PTools::SDF::SDF param method. It may be easier to think of this if you build a compound record index as shown in this next example.
@idx = ('prodnbr',$prodNbrValue);
$fieldValue = $sdfObj->index( @idx, 'otherfield' );
$sdfObj->index( @idx, 'otherfield', "New Value" );
Any other params that you would normally use with the param method are sent along through the index method. The return is the same as returned by param. See the PTools::SDF::SDF manpage.
Finally, when calling the index method on a compound index, pass a ``compound'' value for both the idxFieldName and the idxFieldValue parameters.
Example:
@idx = ( 'prodnbr&xyzzy', $prodNbrValue ."&". $xyzzyValue );
$value = $sdfObj->index( @idx, "otherfieldname" );
$sdfObj->index( @idx, "otherfieldname", "New Value" );
The parameters are identical to the first two parameters to the index method, above.
Example:
@idx = ('prodnbr',$prodNbrValue);
$recNum = $sdfObj->recNumber( @idx );
WARNING: Modifying data values in the returned hash reference will update the values in the corresponding data record.
The parameters are identical to the first two parameters to the index method, above.
This method is equivalent in function to the getRecEntry method in the PTools::SDF::SDF class.
Example:
@idx = ('prodnbr',$prodNbrValue);
$hashRef = $sdfObj->recData( @idx );
$hashRef->{fieldname} = "new value"; # updates the $sdfObj, too.
The parameter is identical to the first parameter to the index method, above.
Example:
$hashRef = $sdfObj->getIndex( 'prodnbr' );
The parameter is identical to the first parameter to the index method, above.
Example:
$idxCount = $sdfObj->indexCount( 'prodnbr' )
The parameters are identical to the first three parameters to the index method, above.
Example:
# Delete the value for the 'proddesc' field in the record
# specified by the "@idx" array.
@idx = ('prodnbr',$prodNbrValue);
$sdfObj->indexDelete( @idx, 'proddesc');
The sort Options are passed through unchanged to whatever sort class happens to be in use with the current object.
Currently this method will not reestablish prior index keys so either perform any ncessary sorting prior to index initilization, or reinitialize indices after each sort.
This PTools::SDF::IDX class inherits from the PTools::SDF::SDF concrete base class. Additional methods are available via this parent class.
The PTools::SDF::DSET class inherits from this class.
See the PTools::SDF::Overview manpage, the PTools::SDF::ARRAY manpage, the PTools::SDF::CSV manpage, the PTools::SDF::DB manpage, the PTools::SDF::DIR manpage, the PTools::SDF::DSET manpage, the PTools::SDF::File manpage, the PTools::SDF::INI manpage, the PTools::SDF::TAG manpage, the PTools::SDF::SDF manpage the PTools::SDF::Lock::Advisory manpage, the PTools::SDF::Sort::Bubble manpage, the PTools::SDF::Sort::Quick manpage and the PTools::SDF::Sort::Shell manpage.
In addition, several implementation examples are available.
See the PTools::SDF::DSET manpage, the PTools::SDF::File::AutoHome manpage, the PTools::SDF::File::Mnttab manpage and the PTools::SDF::File::Passwd manpage. These are contained in the 'PTools-SDF-File-Cmd' distribution available on CPAN
Chris Cobb, <nospamplease@ccobb.net>
Copyright (c) 1999-2007 by Chris Cobb. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|
PTools::SDF::IDX - Allow user-defined indices into PTools::SDF::SDF objects |