Alpha Video Training
Results 1 to 11 of 11

Thread: Need code for outputting 10 random records to a new file - - from any file structure

  1. #1
    Member
    Real Name
    Jose L Garcia
    Join Date
    Sep 2007
    Posts
    13

    Default Need code for outputting 10 random records to a new file - - from any file structure

    We need to create a code for outputting 10 random records to a new file, We will need to use this code on files that have different structure (meaning different field name or field count) which creates a problem since the field count and field name need to be listed if the copy or append operation is used. We appreciate any input on this. Thank you!

    This is the program as it was used in Clipper…
    parameters datafile
    outfile=datafile+"n"

    use &datafile
    copy to &outfile for recno()=int(reccount()/10*1).or.;
    recno()=int(reccount()/10*2).or.;
    recno()=int(reccount()/10*3).or.;
    recno()=int(reccount()/10*4).or.;
    recno()=int(reccount()/10*5).or.;
    recno()=int(reccount()/10*6).or.;
    recno()=int(reccount()/10*7).or.;
    recno()=int(reccount()/10*8).or.;
    recno()=int(reccount()/10*9).or.;
    recno()=int(reccount()/10*10)

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

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    Good morning, Jose.

    This kind of question probably belongs in the forum corresponding to the version of Alpha Five you're using. It will get better exposure there, and idiosyncracies based on your version number will be minimized.

    Please clarify or confirm what your input source will be, and what format you want the output target to be. I'm not a clipper head but it seems your goal is to take any arbitrary table (not file) and copy a random selection of records to a new table (not file). Is this right?

  3. #3
    Member
    Real Name
    Jose L Garcia
    Join Date
    Sep 2007
    Posts
    13

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    Tom –
    Good morning and thanks for your reply.

    Yes, you are correct about my goal - (table) should be the word.

    The input and output source will be the same format although at some point I will need to convert both to comma delimited.

    This is my table source and destination code so far…

    tbl_source = table.open("c:\temp\o"+alltrim(runjob->Jobnum)+"1.dbf", FILE_RW_EXCLUSIVE)

    tbl_dest = table.open("c:\temp\o"+alltrim(runjob->Jobnum)+"1n.dbf", FILE_RW_EXCLUSIVE)

  4. #4
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,119

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    The random records part is easy since Alpha has a built in random_records() function to use in a query. The tedious part will be iterating through the source table fields with

    tbl.fields_get - to get the count
    fld = tbl.field_get(#) - to get a pointer to each field, consecutively
    fld.name_get() - to get each field's name

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

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    Jose,

    You should investigate using the "Copy" operation. I believe if create one using the designer you'll be able to copy the code that's generated by the designe and then adapt it to your situation. The trick will be coming up with a filter expression that generates a random selection of records. But to begin with read up on "operations", and especially the "copy" operation.

    Later...
    Hey Stan, good morning. I forgot about the random_records() function. Thanks for mentioning it.

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

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    Stan,

    if your just looking for the list of fields names you can use

    tbl.field_name_get()
    Tim Kiebert
    Eagle Creek Citrus
    A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

  7. #7
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,119

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    Quote Originally Posted by Tim Kiebert View Post
    Stan,

    if your just looking for the list of fields names you can use

    tbl.field_name_get()
    Yeah, and I just remembered

    tbl.create_clone()

    which obviates the need for all that.

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

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    Jose,

    You may also want ot check out the <TBL>.COPY_RECORDS_TO() function. It takes a filter in which you can use the Random_Records() function Stan mentioned.

    EDIT: YEP, good one Stan.

    Maybe first tbl.create_clone() and then tbl.copy_records_to() the newly cloned table.
    Tim Kiebert
    Eagle Creek Citrus
    A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

  9. #9
    Member
    Real Name
    Jose L Garcia
    Join Date
    Sep 2007
    Posts
    13

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    Thanks everyone for your replies, please see the code below... for some reason I get this message "table structures do not match. cannot copy records" Please let me know if you have any suggestions. Thanks


    dim tbl_source as P
    dim filename as C
    dim qry as P

    tbl_source = table.open("c:\temp\o"+alltrim(runjob->Jobnum)+"1.dbf")
    filename = "c:\temp\o"+alltrim(runjob->Jobnum)+"1n.dbf"
    tbl_source.create_clone(filename)

    tbl_source = table.current()

    query.description = "nth"
    query.order = ""
    query.filter = random_pct_record_list(10)
    query.options = ""
    qry = tbl_source.query_create()
    tbl_dest = tbl_source.copy_records_to(filename,"nth")

  10. #10
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,119

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    Quote Originally Posted by JLG View Post
    Thanks everyone for your replies, please see the code below... for some reason I get this message "table structures do not match. cannot copy records" Please let me know if you have any suggestions. Thanks


    dim tbl_source as P
    dim filename as C
    dim qry as P

    tbl_source = table.open("c:\temp\o"+alltrim(runjob->Jobnum)+"1.dbf")
    filename = "c:\temp\o"+alltrim(runjob->Jobnum)+"1n.dbf"
    tbl_source.create_clone(filename)

    tbl_source = table.current()

    query.description = "nth"
    query.order = ""
    query.filter = random_pct_record_list(10)
    query.options = ""
    qry = tbl_source.query_create()
    tbl_dest = tbl_source.copy_records_to(filename,"nth")
    You have two lines which do the same thing.

    Code:
    tbl_source = table.open("c:\temp\o"+alltrim(runjob->Jobnum)+"1.dbf")filename = "c:\temp\o"+alltrim(runjob->Jobnum)+"1n.dbf"
    tbl_source.create_clone(filename)
    
    tbl_source = table.current()
    I think the second one is causing the trouble.

  11. #11
    Member
    Real Name
    Jose L Garcia
    Join Date
    Sep 2007
    Posts
    13

    Default Re: Need code for outputting 10 random records to a new file - - from any file struc

    This is what i was looking for. Thanks everyone.

    tbl_source.create_clone(filename)
    result = tbl_source.copy_records_to(filename,"random_records(10,\".t.\")" )

Similar Threads

  1. Jpeg images as part of the file structure
    By Billn in forum Alpha Five Version 7
    Replies: 7
    Last Post: 04-17-2006, 11:39 AM
  2. File Structure Update
    By mikedg in forum Alpha Five Version 6
    Replies: 2
    Last Post: 10-01-2005, 01:18 AM
  3. Emulating Windows folder/file structure in A5
    By MS in forum Alpha Five Version 5
    Replies: 9
    Last Post: 01-31-2003, 03:39 PM
  4. .sem file set structure help needed
    By Peggy Davis in forum Alpha Five Versions 2 and 3
    Replies: 2
    Last Post: 06-03-2002, 07:19 PM

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
  •