locked
Number generator RRS feed

  • Question

  • Hello Forum,

    I am using fox 2.6 DOS. I have a 7 position Char field called "Number". We would like to start at a given number say 1000000 and then continue to add one to that field to an assigned number say 1000499. So record one would be 1000000, record 2 would be 1000001 and so on. then the last number would be 1000499.

    Thank you!!

    Glenn


    Glenn

    Wednesday, November 20, 2013 12:44 AM

Answers

  • Those lines are only to add new records, that is the purpose of APPEND BLANK.
    I still don't know what are you trying to do/accomplish...

    If you would like to:
    * Change beginning and ending number, 
      1) you only have to update ln_start and ln_end (inside the program), the FOR ... NEXT will do the rest
      2) update ln_start and ln_end via parameter 
          i.e. DO <<program name>> WITH 1000000, 1000499     && I am using the original numbers, of course, you can change the numbers...
          your program will look something like       

    *- program name
    LPARAMETER ln_start, ln_end
    
    FOR ln_loop = ln_start TO ln_end
      append blank   && was missing before
      replace YourCharField with STR(ln_loop,7)
    NEXT 
    
    RETURN

      3) you can add an index, and make a search, if you want to validate for the number to be new or just to look for the last entry

    *- Assuming your table has an index based on VAL(YourCharField)
    Open your table with the index selected
     
    GO BOTTOM   && Get the last value
    lc_next  = STR( VAL(YourCharField) +1,7,0)
    
    append blank   && was missing before
    replace YourCharField with lc_next    && Already character
    
    RETURN


      4) etc.


    As I said, depending the purpose, how you would approach it




    • Edited by Benny Gabel Monday, November 25, 2013 12:16 AM
    • Marked as answer by gh449 Monday, November 25, 2013 11:11 AM
    Monday, November 25, 2013 12:05 AM
  • If I understood correctly, would be something like this

    ln_start = 1000000
    ln_end   = 1000499
    
    FOR ln_loop = ln_start TO ln_end
      ? ln_loop
    NEXT   

    • Marked as answer by gh449 Friday, November 22, 2013 12:42 AM
    Wednesday, November 20, 2013 1:02 AM

All replies

  • If I understood correctly, would be something like this

    ln_start = 1000000
    ln_end   = 1000499
    
    FOR ln_loop = ln_start TO ln_end
      ? ln_loop
    NEXT   

    • Marked as answer by gh449 Friday, November 22, 2013 12:42 AM
    Wednesday, November 20, 2013 1:02 AM
  • If you have a character field, you can always extract the numeric value of it
    lc_value = "1000000"
    ln_value = VAL(lc_value)

    IF ln_value < 1000499
       lc_next  = STR(ln_value+1,7,0)
    ENDIF 


    • Proposed as answer by Pavel Celba Wednesday, November 20, 2013 11:02 PM
    Wednesday, November 20, 2013 4:04 PM
  • The loop command is missing but the conversion should work...
    Wednesday, November 20, 2013 11:04 PM
  • ln_start = 1000000
    ln_end   = 1000499
    FOR ln_loop = ln_start TO ln_end
      replace YourCharField with str(ln_loop,7)
    NEXT 

    Thursday, November 21, 2013 9:24 AM
    Answerer
  • I ran this and all I got was record 1 that contained 1000499 the rest of the records were blank?

    Any thoughts?

    Thanks

    Glenn


    Glenn

    Friday, November 22, 2013 12:46 AM
  • On RavindraPatil code, there is no "new record generation", if you want to create new records you have to add APPEND BLANK before the REPLACE, otherwise, will replace over the same record. The code would look like the following

    ln_start = 1000000
    ln_end   = 1000499
    FOR ln_loop = ln_start TO ln_end
      append blank   && was missing before
      replace YourCharField with str(ln_loop,7)
    NEXT 

    But again, that is in the case you "just" want to generate new records with numbers. Let me know if you have any other question.


    • Edited by Benny Gabel Friday, November 22, 2013 2:24 AM
    Friday, November 22, 2013 2:23 AM
  • your changes worked to and added the number string to the table but it started numbering at the end of the file then ran for 499 times creating 499 new records with only the "number" field populated with the new number generated.

    Can we try one more time. what am I missing?

    THANK YOU!!!


    Glenn

    Sunday, November 24, 2013 11:43 PM
  • Those lines are only to add new records, that is the purpose of APPEND BLANK.
    I still don't know what are you trying to do/accomplish...

    If you would like to:
    * Change beginning and ending number, 
      1) you only have to update ln_start and ln_end (inside the program), the FOR ... NEXT will do the rest
      2) update ln_start and ln_end via parameter 
          i.e. DO <<program name>> WITH 1000000, 1000499     && I am using the original numbers, of course, you can change the numbers...
          your program will look something like       

    *- program name
    LPARAMETER ln_start, ln_end
    
    FOR ln_loop = ln_start TO ln_end
      append blank   && was missing before
      replace YourCharField with STR(ln_loop,7)
    NEXT 
    
    RETURN

      3) you can add an index, and make a search, if you want to validate for the number to be new or just to look for the last entry

    *- Assuming your table has an index based on VAL(YourCharField)
    Open your table with the index selected
     
    GO BOTTOM   && Get the last value
    lc_next  = STR( VAL(YourCharField) +1,7,0)
    
    append blank   && was missing before
    replace YourCharField with lc_next    && Already character
    
    RETURN


      4) etc.


    As I said, depending the purpose, how you would approach it




    • Edited by Benny Gabel Monday, November 25, 2013 12:16 AM
    • Marked as answer by gh449 Monday, November 25, 2013 11:11 AM
    Monday, November 25, 2013 12:05 AM
  • Hi Glenn,

    I don't know if you're still looking for a solution, but it's pretty straightforward ... assuming you're starting with an empty file (no records). If you already have those 500 records in the file, and you're wanting to fill the NUMBER field as you specified, then you'll need to use a different method. But, assuming the empty file scenario, this routine will do the job. (Note: the lines that begin with "*" are comments that don't need to be included.)

    * Replace "SAMPLE" in the following line with your actual filename

    SELECT SAMPLE
    DISPLAY STRUCTURE

    *

    FOR COUNT = 1000000 TO 1000499
      APPEND BLANK
      REPLACE NUMBER WITH STR(COUNT, 7)
    NEXT

    *

    GOTO TOP
    LIST NEXT 10

    GOTO BOTTOM
    SKIP -9
    LIST NEXT 10

    * Here's the output from the program:

    * Structure for database: SAMPLE.DBF
    * Number of data records:       0                            
    * Date of last update   : 11/25/2013
    * Code Page             : 0                                  
    * Field  Field Name  Type       Width    Dec    Index  Collate
    *     1  NUMBER      Character      7                        
    * ** Total **                       8                        
    *
    * Record#  NUMBER
    *       1  1000000
    *       2  1000001
    *       3  1000002
    *       4  1000003
    *       5  1000004
    *       6  1000005
    *       7  1000006
    *       8  1000007
    *       9  1000008
    *      10  1000009
    *
    * Record#  NUMBER
    *     491  1000490
    *     492  1000491
    *     493  1000492
    *     494  1000493
    *     495  1000494
    *     496  1000495
    *     497  1000496
    *     498  1000497
    *     499  1000498
    *     500  1000499

    Tuesday, November 26, 2013 2:40 AM
  • Hello again Glenn,

    After reading through the series of posts on this thread another time, it now seems clear to me that you already have the records in your file, and you want to put the ascending sequence of numbers into your preexisting character field called "Number". For this type of task, I typically use FoxPro's "SCAN" command.

    Ok, assuming you have 500 pre-existing records, here's a code example that will assign the numbers.

    * Replace "SAMPLE" in the following line with your actual filename

    SELECT SAMPLE
    GOTO TOP
    DISPLAY STRUCTURE

    *

    COUNT = 999999

    SCAN NEXT 500
      COUNT = COUNT + 1
      REPLACE NUMBER WITH STR(COUNT, 7)
    ENDSCAN

    *

    GOTO TOP
    LIST NEXT 10

    GOTO BOTTOM
    SKIP -9
    LIST NEXT 10

    * Here's the output from the program:

    * Structure for database: SAMPLE.DBF
    * Number of data records:     500                            
    * Date of last update   : 11/25/2013
    * Code Page             : 0                                  
    * Field  Field Name  Type       Width    Dec    Index  Collate
    *     1  NUMBER      Character      7                        
    * ** Total **                       8                        
    *
    * Record#  NUMBER
    *       1  1000000
    *       2  1000001
    *       3  1000002
    *       4  1000003
    *       5  1000004
    *       6  1000005
    *       7  1000006
    *       8  1000007
    *       9  1000008
    *      10  1000009
    *
    * Record#  NUMBER
    *     491  1000490
    *     492  1000491
    *     493  1000492
    *     494  1000493
    *     495  1000494
    *     496  1000495
    *     497  1000496
    *     498  1000497
    *     499  1000498
    *     500  1000499

    Tuesday, November 26, 2013 5:45 AM
  • Just my simple $.02. How 'bout

    SET ORDER TO 0

    REPLACE ALL Number WITH TRANS(RECNO()+999999)

    Am I missing something? Are there deleted records not included? If so, see above.

    Steve

    Tuesday, November 26, 2013 3:56 PM