Alpha DevCon 2018
Results 1 to 16 of 16

Thread: Iterate Index Keys ?

  1. #1
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,300

    Default Iterate Index Keys ?

    Is there a way to fetch through the index keys? I'm finding that <index>.find_next() always returns a pointer to the index key that's "next" in relation to the tables record pointer. Can't use it to step through the keys in the index (without also stepping through the records in the table). Am I missing something here?

    Any suggestions on how to "find" a key, and then step forward through the index list from that point on?

    Thanks.

    -- tom

  2. #2
    VAR
    Real Name
    Martin W. Cole
    Join Date
    Apr 2000
    Location
    Terrell, Texas (near Dallas)
    Posts
    5,939

    Default Re: Iterate Index Keys ?

    Tom, as for actual indexes for the table (not temporary ones) you could use the script Selwyn gave us to save the indexes for all tables to a text file - I save the indexes to a table instead of a text file, with the tablename as the first field, and the indexes in a memo - so I can fetch a specific table and get the indexes from the memo - although a bit of a kludge, it will work
    Cole Custom Programming - Terrell, Texas
    972 524 8714
    martin_w_cole@msn.com

    ____________________
    "A young man who is not liberal has no heart, but an old man who is not conservative has no mind." GB Shaw

  3. #3
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,300

    Default Re: Iterate Index Keys ?

    Thanks, Martin.

    I'm not trying to enumerate the index tag specifications.

    I'm trying to "fetch" through the keys for a specific index without fetching records in the related table. i.e find_first(), then find_next() over and and over until there are no more keys in the specific index list.

  4. #4
    Member
    Real Name
    James Miles
    Join Date
    Jan 2008
    Location
    Krakatau East of Java
    Posts
    241

    Default Re: Iterate Index Keys ?

    Tom;

    Did you try some "sort" of application of <tbl>.cursor_get()
    Since when you move the pointer in a cursor you do not change the record that the base table (open) pointer is pointing to. ?

  5. #5
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,300

    Default Re: Iterate Index Keys ?

    James, no, haven't considered that.

    a) I don't understand the cursor_get() method and can't find it's documentation; and

    b) I am trying not to fetch records from the table as I step through the index.

    My goal is to be able to jump to an index key in a specific index, and then build a list of the next 100 index keys from there as fast as possible. I'd like to avoid clogging the network with fetches to the table itself.

  6. #6
    "Certified" Alphaholic
    Real Name
    Tom Baker
    Join Date
    Jun 2006
    Location
    Near Cherry Hill, NJ
    Posts
    1,296

    Default Re: Iterate Index Keys ?


  7. #7
    "Certified" Alphaholic Tim Kiebert's Avatar
    Real Name
    Tim Kiebert
    Join Date
    Jul 2004
    Location
    Geelong, Victoria, Australia
    Posts
    2,784

    Default Re: Iterate Index Keys ?

    I think James meant <tbl>.get_cursor()

    This is another item that has not yet made it into the regular help file.

    Its usage is outlined in the release notes for the patches.
    Build 1471-3180 — 9-May-2008
    Tim Kiebert
    Geelong Citrus Packers
    A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

  8. #8
    Member
    Real Name
    James Miles
    Join Date
    Jan 2008
    Location
    Krakatau East of Java
    Posts
    241

    Default Re: Iterate Index Keys ?

    Sorry about that;

    I did mean <tb>.get_cursor(). Was just going by memory, should have check first.

  9. #9
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,300

    Default Re: Iterate Index Keys ?

    Thanks for the link to the release notes. I'm not sure this new table method will avoid the network traffic that will otherwise occur if one steps through all the records in a large table using the cursor. Hard to imagine that it might.

    -- tom

  10. #10
    VAR
    Real Name
    Martin W. Cole
    Join Date
    Apr 2000
    Location
    Terrell, Texas (near Dallas)
    Posts
    5,939

    Default Re: Iterate Index Keys ?

    Tom, it sounds almost like you want to open the "cdx" file for a table and read the entries that way. Short of getting someone like Selwyn to write a script with commands that they have available we don't have or don't know about, you might want to consider a query - since the query is saved locally, containing the keys, and you could thus fetch through the query without network traffic. The data has to either be local or in a memory buffer - how else could you do it?
    Cole Custom Programming - Terrell, Texas
    972 524 8714
    martin_w_cole@msn.com

    ____________________
    "A young man who is not liberal has no heart, but an old man who is not conservative has no mind." GB Shaw

  11. #11
    Member
    Real Name
    Steve Andrews
    Join Date
    Jan 2004
    Location
    Mahwah, NJ
    Posts
    644

    Default Re: Iterate Index Keys ?

    Quote Originally Posted by Tom Cone Jr View Post
    I'm finding that <index>.find_next() always returns a pointer to the index key that's "next" in relation to the tables record pointer. Can't use it to step through the keys in the index (without also stepping through the records in the table).-- tom
    Tom,

    I'm failing to see why the find_next() cannot be used. I ran the code below and it went through every key of the index "Dept" without loading table records.

    Code:
    dim pFirst as P
    dim pLast as P
    dim pKey as P
    
    tbl = table.open("codes")
    ndx = tbl.index_primary_put("Depts")
    
    pLast = ndx.find_last()
    pFirst = ndx.find_first()
    
    pKey = ndx.key_get()
    ui_msg_box("Started",str(pKey.Record))
    
    while pKey.Record <> pLast.Record
    	pKey = ndx.find_next(pKey)
    	ui_msg_box("Next",str(pKey.Record))
    wend	
    
    tbl.close()
    Am I misunderstood in what you are trying to achieve?

    Steve

  12. #12
    "Certified" Alphaholic Tim Kiebert's Avatar
    Real Name
    Tim Kiebert
    Join Date
    Jul 2004
    Location
    Geelong, Victoria, Australia
    Posts
    2,784

    Default Re: Iterate Index Keys ?

    Tom, the following code runs in Alphasports. It should show that the index gets looped through without moving the record pointer of the table.

    Code:
    dim tbl as P
    tbl = table.open("customer")
    
    indx = tbl.index_primary_put("Lastname1")
    
    lst = indx.find_last()
    b= indx.find("Smith")
    
    msgbox("Find 'Smith' in Index, Table pointer not moved",b.key  + b.record + crlf(2)+ tbl.Lastname +tbl.recno())
    
    tbl.fetch_first()
    
    msgbox("Find 'Smith' in Index, Table pointer moved to first record","In this case the first record is according to the index used." + crlf(2) +\
            b.key  + b.record + crlf(2)+ tbl.Lastname +tbl.recno())
    For i = 1 to 10
        if b.record =lst.record then
            exit for
        end if
        b=indx.find_next(b)
        msgbox("Find next key in Index, Table pointer stays on the first record","Loop number " + i+crlf(2)+ \
                b.key  + b.record + crlf(2)+ tbl.Lastname +tbl.recno())
    Next
    
    tbl.close()
    I see Steve beat me to it. Must have been those extra message boxes I threw in there. :)
    Last edited by Tim Kiebert; 07-13-2009 at 11:57 AM.
    Tim Kiebert
    Geelong Citrus Packers
    A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

  13. #13
    "Certified" Alphaholic Tim Kiebert's Avatar
    Real Name
    Tim Kiebert
    Join Date
    Jul 2004
    Location
    Geelong, Victoria, Australia
    Posts
    2,784

    Default Re: Iterate Index Keys ?

    In light of Martin's comments, I think even my previous post won't eliminate network traffic as I think each read of the index would still mean a trip to the server. Albeit with a lot less data to transmit. You could bring all the keys over in one trip using the <INDEX>.KEY_LIST_GET() method. With something like the following which would result in a variable holding all the keys. Then you would need to use character functions to do teh remainder of the processing. Maybe even populate an array.
    Code:
    dim tbl as P
    dim indx as P
    dim keylist as C
    tbl = table.open("customer")
    
    indx = tbl.index_primary_put("Lastname1")
    keyList = indx.key_list_get(indx.records_get(),1,"K"+crlf())
    msgbox(keylist)
    
    tbl.close()
    Tim Kiebert
    Geelong Citrus Packers
    A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

  14. #14
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,300

    Default Re: Iterate Index Keys ?

    Thanks, guys. It turns out I was using the find_next() method incorrectly. I was passing an index pointer to the method, instead of a pointer to the key. I appreciate the help. This will get me where I need to go (barring other excursions into the land of badly written code!).

    Tim, I am not trying to eliminate network traffic, just minimize it, cause I am trying to populate a listview control as fast as possible. I'm thinking that if the index key is all that needs to be stuffed into the control, then it's faster to pull it directly from the index, than from the table, especially in cases where the record structure in the table is pretty big and the record count is high.

    -- tom

  15. #15
    "Certified" Alphaholic Tim Kiebert's Avatar
    Real Name
    Tim Kiebert
    Join Date
    Jul 2004
    Location
    Geelong, Victoria, Australia
    Posts
    2,784

    Default Re: Iterate Index Keys ?

    Quote Originally Posted by Tom Cone Jr View Post
    Tim, I am not trying to eliminate network traffic, just minimize it, cause I am trying to populate a listview control as fast as possible. I'm thinking that if the index key is all that needs to be stuffed into the control, then it's faster to pull it directly from the index, than from the table, especially in cases where the record structure in the table is pretty big and the record count is high.

    -- tom
    I kind of thought as much but figured I would throw the idea into the mix.

    Cheers
    Tim Kiebert
    Geelong Citrus Packers
    A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

  16. #16
    "Certified" Alphaholic MoGrace's Avatar
    Real Name
    Robin
    Join Date
    Mar 2006
    Location
    Los Angeles
    Posts
    3,455

    Default Re: Iterate Index Keys ?

    When it takes me more than an hour to find something in here due to broken links, I feel I should share my discoveries...

    I have a form with an embedded browse that is difficult to see when the last record in the browse has been reached when the user moves thru the records using a navigation button. So I wanted to add a sound to notify the user he was on the last record so he knows to move to the next parent record.

    Here are the 2 methods I found useful:

    lrec = browse1.Index_pointer().Find_Last().record

    ?typeof(lrec)
    = "N"

    ?lrec
    = 50

    cur_rec = browse1.Table_Get().recno()

    ?cur_rec
    = 13

    So I set these up as shared variables, setting the 1st in the OnFetch event of the form, and the 2nd in the OnRowChange event in the browse - where I test if they match and if so, do a ui_beep().

    Now when the user fetches a new parent record the first function gets the new last record number for the browse. Then regardless of how the browse is navigated (using a button or not), when the last record in the browse is reached the two variables will match and the system will now beep!

    btw searching the Wiki for 'find_last' does produce a couple of links for index or query - but the scripts are more complex than this was - Which I found searching the browse1 methods for the form in the IW.
    Last edited by MoGrace; 01-07-2018 at 04:20 PM.
    Robin

    Discernment is not needed in things that differ, but in those things that appear to be the same. - Miles Sanford

Similar Threads

  1. How to iterate through form controls
    By dodgebros in forum Alpha Five Version 7
    Replies: 10
    Last Post: 04-01-2008, 03:50 PM
  2. index.a5w or index.html
    By Dan Blank in forum Web Application Server v6
    Replies: 3
    Last Post: 01-21-2005, 11:34 AM
  3. F keys
    By Chuck Mitchell in forum Alpha Five Version 5
    Replies: 8
    Last Post: 12-15-2003, 08:18 PM
  4. Help with Send Keys
    By Trimcom in forum Alpha Five Version 4
    Replies: 5
    Last Post: 02-05-2001, 08:06 PM
  5. Control keys (hot keys)
    By Lowell Wardlow in forum Alpha Five Version 4
    Replies: 0
    Last Post: 09-25-2000, 08:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •