Author Topic: hope this routine is as good as i think it is  (Read 3134 times)

0 Members and 1 Guest are viewing this topic.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
hope this routine is as good as i think it is
« on: October 06, 2020, 04:44:35 pm »
Hello

this section of code will increment the record number by 1. I will covert the number to string and add zeros to it up to 9 to make it an id number in the format of 000000001 and save it as a string.

This will make and keep the id numbers in ascending order in sequence .

to to make an index of id and any information for the file you need to search on it will be in order by id number
i hope i got my idea across lol
Badger

not a master programmers touch but for me its pretty good

variables z is integer64, l is long, s is string, i is integer
ifilenum =1
zcustrecnum at start will be 1
lcustreclen is 256

Code: QB64: [Select]
  1. autoindex:
  2. SELECT CASE ifilenum
  3.         CASE 1:
  4.                 zcustrecnum = LOF(1) / lcustreclen
  5.                 zcustrecnum = zcustrecnum + 1
  6.                 sindex = LTRIM$(STR$(zcustrecnum))
  7.                 IF LEN(sindex) < 9 THEN
  8.                         sindex = STRING$(9 - LEN(sindex), 48) + sindex
  9.                         scustinforec.id = sindex
  10.                 END IF
  11.  
  12.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: hope this routine is as good as i think it is
« Reply #1 on: October 06, 2020, 05:18:55 pm »
That's perfectly good, except for maybe one question, which after I will ask you after I show you these...

Here are a couple of alternatives, using fewer variables.

Code: QB64: [Select]
  1. DIM scustinforec.id AS STRING
  2. zcustrecnum& = 123
  3. IF zcustrecnum& <= 999999999 THEN
  4.     scustinforec.id = STRING$(9 - LEN(LTRIM$(STR$(zcustrecnum&))), 48) + LTRIM$(STR$(zcustrecnum&))
  5.     PRINT scustinforec.id
  6.  
  7. scustinforec.id = STRING$(9, "0")
  8. IF zcustrecnum& < 999999999 THEN
  9.     MID$(scustinforec.id, 10 - LEN(LTRIM$(STR$(zcustrecnum&)))) = LTRIM$(STR$(zcustrecnum&))
  10.     PRINT scustinforec.id
  11.  

Just for fun, I used the numeric value, rather than converting it to a string length. Now, the important part, how did you dim the data tye for your variable: zcustrecnum? If you are really expecting to run up to 9-digits, you need a data type that won't end up in your string as scientific notation. For instance...

Code: QB64: [Select]
  1. a = 999999999
  2.  
  3. a% = 999999999 ' Integer data type
  4. PRINT a%, LTRIM$(STR$(a%))
  5.  
  6. a! = 999999999 ' Single data type
  7. PRINT a!, LTRIM$(STR$(a!))
  8.  
  9. a& = 999999999 ' Long data type
  10. PRINT a&, LTRIM$(STR$(a&))
  11.  
  12. a# = 999999999 ' Double data type
  13. PRINT a#, LTRIM$(STR$(a#))
  14.  

Of the data types I listed, only the Long (&) and Double (#) will work for a 9-digit number. There is also _INTEGER64, which would be && and can handle 18-digit numbers.
 
Check out the data types in the QB64 wiki, if you're not sure what I'm talking about here.
http://www.qb64.org/wiki/Data_types

Naturally, you don't have to use the type suffix if you use DIM or make the variable a TYPE.

Pete

« Last Edit: October 06, 2020, 05:42:55 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: hope this routine is as good as i think it is
« Reply #2 on: October 06, 2020, 05:38:48 pm »
Hello

and you are correct i am going to use 4 different instances of this. I have scustinfored, senventoryrec, sposalerec, ssystemrec. Except for ssystemrec they all have .id that cant have duplicates so i did it this way. once done i will look at what you did closer to see if i can combine and shrink the code.

Badger

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: hope this routine is as good as i think it is
« Reply #3 on: October 06, 2020, 05:40:58 pm »
Hello

Forgot to answer your question anything thats starts with a z is dim as _integer64

Badger

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: hope this routine is as good as i think it is
« Reply #4 on: October 06, 2020, 05:45:11 pm »
Then get busy... Your wife will be able to fill up to a quintillion orders with that data type!

Pete :D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: hope this routine is as good as i think it is
« Reply #5 on: October 06, 2020, 05:47:37 pm »
Hello

Lmao she always says i do a little overkill

she will never use it but i like to program it to just say its there.

Badger