[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference noted::hackers_v1

Title:-={ H A C K E R S }=-
Notice:Write locked - see NOTED::HACKERS
Moderator:DIEHRD::MORRIS
Created:Thu Feb 20 1986
Last Modified:Mon Aug 03 1992
Last Successful Update:Fri Jun 06 1997
Number of topics:680
Total number of notes:5456

344.0. "Finding highest/lowest key value in a file" by UBEAUT::MACKAY (Don Mackay) Wed Oct 29 1986 22:39

    In an application I am working on, I would like to know what the
    largest value of an ascending key field is (alternastively the smallest
    value of a descending key field). I have thought of several ways
    of doing this:
    
    1 - read the file sequentially (YUK!!!!)
    
    2 - read the first record, then start in steps of 1,000,000,000
    (or some other suitably large number) and try for a record, stepping
    further forward for each successful read, dividing the step by 10
    for each failure
    
    3 - <space for further suggestions???>
    
    Anyone have any good ideas on how to do this (I'm not averse to
    doing a few hacks but I would prefer to stay with 'supported' methods
    for maintainability)???
    
    Thanks
    
    Don Mackay
T.RTitleUserPersonal
Name
DateLines
344.1The Lowest I Can Do, But The Highest...??KIRK::NORTONCharles McKinley Norton | BasewayThu Oct 30 1986 16:5627
About Finding The Lowest Key
----------------------------

Finding the lowest key is not too bad. If the key you are using to
read the file is a binary key, then start reading with its value set
to zero.  If the key is a character key, then the character string
must be initialized to a null string. You cannot get a null string
this way (at least in PL/I):

	character_string_variable = ' ';

	character_string_variable = '';

		
After reading your note, I posted a note in PL/I (TLE::PLI) and Pascal
(TLE::PASCAL) notes files, because I have had occaision when this sort
of thing would be useful.  You could check those notes files for
responses to my note.

About Finding The Highest Key
-----------------------------

It does not sound like you can do it easily, but your suggestion 2
certainly sounds better than reading the entire file either
sequentially, or reading with match_greater until you get an RMS$_RNF.

CMN CMPD Product Development | MLO5-2/E50 Pole 36A | 223-3590
344.2binary successive approximation?PHENIX::SMITHWilliam P.N. (Wookie::) SmithThu Oct 30 1986 17:075
    Well, if you are going to use method 2, make sure you divide by
    2 everytime you shrink your search area.
    
    Willie
    
344.3ULTRA::PRIBORSKYTony PriborskyThu Oct 30 1986 18:518
    Doesn't RMS have "key GEQ" or "key LEQ" find operations?   If so,
    to find the lowest key, look for the "key GEQ 'low value'" (where
    "low value" has real meaning if you program in COBOL, but is zero
    for a binary key (in the proper format, integer, floating etc.),
    and the null string for a string key.)   To find the highest key,
    search for the "key LEQ 'high value'".   I'm sure I used such tricks
    from BASIC on the PDP-11's many moons ago.  Similar things should
    be available in some language that runs under VMS.
344.4No easy wayQUARK::LIONELReality is frequently inaccurateThu Oct 30 1986 23:1021
    There is no way to traverse an indexed file key in reverse order
    - the indices just aren't built with backwards pointers.  So for
    the traditional ascending key types, you can only read sequentially
    through the file in ascending order - for the new descending key
    types, you can only traverse in descending order.  Furthermore,
    RMS provides no way to directly locate the "last" record in an index
    (just as it provides no way to read the last record in a sequential
    file).
    
    The binary search method is reasonable, as long as all you want
    to do is to find the "last" record.  If you truly want to read the
    file in reverse order (something the base note did not request,
    but others have), the only real possibility is to read all the records,
    store the RFAs, and then go backwards through your RFA list.  VAX
    FORTRAN's BACKSPACE statement uses a variation of this method.
    
    Of course, if you have the liberty of designing the file, simply
    define two overlapping key fields, one ascending, one descending,
    and then you can go whichever way you desire.
    
    					Steve
344.5CLT::GILBERTeager like a childFri Oct 31 1986 18:005
>   the only real possibility is to read all the records,
>   store the RFAs, and then go backwards through your RFA list.

The MATH conference on CLT:: discussed the problem of minimizing the number
of I/Os, given a limited number of RFAs that could be cached at once.
344.6Sideways access to highest keyTHEBAY::HAYESSame stuff, different DayWed Nov 12 1986 21:2717
    
    1]  Finding lowest key
    
    	Do a REWIND on the key followed by a sequential GET, or a random
    	GET key GEQ using low values.
    
    2)	Finding highest key
    
    	As said before, RMS does not support a direct way to do this.
     	One method we have used in the past with ascending binary keys
    	is to create a another key field in the record that is set to
    	the complement of the key (and is therefore descending).  Then
    	the trick is to find the lowest key (which is easy), and that
    	record will also contain your highest key.
    
    John