Author Topic: I can't decide which I like best...  (Read 5473 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
I can't decide which I like best...
« on: September 22, 2020, 07:10:14 pm »
I actually kept two ways of determining running accounts of instances of phone numbers in one of my database programs. I seldom if never do that, but I couldn't decide which one I liked the best, so I used each one in two separate parts of the program. Any thoughts of one being better than the other, or maybe another method?

Code: QB64: [Select]
  1. WIDTH 144, 25
  2.     READ mydata$
  3.     IF mydata$ = "EOF" THEN EXIT DO
  4.     j = 1: occ% = 0
  5.     dataconcat$ = mydata$ + "|" + dataconcat$
  6.     DO UNTIL j > LEN(dataconcat$)
  7.         IF INSTR(j, dataconcat$, mydata$ + "|") - j = 0 THEN occ% = occ% + 1
  8.         j = INSTR(j, dataconcat$, "|") + 1
  9.     LOOP
  10.     PRINT mydata$; "#"; LTRIM$(STR$(occ%)); ", ";
  11. dataconcat$ = ""
  12. PRINT "End Method #1."
  13.  
  14.  
  15.     READ mydata$
  16.     IF mydata$ = "EOF" THEN EXIT DO
  17.     j = 0: occ% = 0
  18.     dataconcat$ = mydata$ + "|" + dataconcat$
  19.     DO UNTIL j > LEN(dataconcat$)
  20.         k = INSTR(j, dataconcat$, "|") - j
  21.         IF MID$(dataconcat$, j, k) = mydata$ THEN occ% = occ% + 1
  22.         j = j + k + 1
  23.     LOOP
  24.     PRINT mydata$; "#"; LTRIM$(STR$(occ%)); ", ";
  25. dataconcat$ = ""
  26. PRINT "End Method #2."
  27.  
  28. DATA dog,cat,rabbit,frog,horse,dog,mouse,pig,cat,bat,cat,dog,bird,fish,cat,pig,dog

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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #1 on: September 22, 2020, 07:34:48 pm »
Do you like alphabetic order?

loadsort demo:
Code: QB64: [Select]
  1. REDIM dat$(0)
  2.     READ insert$
  3.     IF insert$ <> "EOD" THEN loadSort insert$, dat$() ELSE EXIT DO
  4. FOR i = LBOUND(dat$) TO UBOUND(dat$)
  5.     IF dat$(i) <> "" THEN
  6.         PRINT dat$(i)
  7.         IF concat$ = "" THEN concat$ = dat$(i) ELSE concat$ = concat$ + ", " + dat$(i)
  8.     END IF
  9. PRINT: PRINT concat$
  10.  
  11. DATA dog,cat,rabbit,frog,horse,dog,mouse,pig,cat,bat,cat,dog,bird,fish,cat,pig,dog,EOD
  12.  
  13. SUB loadSort (insertN AS STRING, dynArr() AS STRING) '  version 2020-06-07
  14.     'note this leaves dynArr(0) empty! so ubound of array is also number of items in list
  15.     DIM ub, j, k
  16.     ub = UBOUND(dynArr) + 1
  17.     REDIM _PRESERVE dynArr(LBOUND(dynArr) TO ub) AS STRING
  18.     FOR j = 1 TO ub - 1
  19.         IF insertN < dynArr(j) THEN '  GT to LT according to descending or ascending sort
  20.             FOR k = ub TO j + 1 STEP -1
  21.                 dynArr(k) = dynArr(k - 1)
  22.             NEXT
  23.             EXIT FOR
  24.         END IF
  25.     NEXT
  26.     dynArr(j) = insertN
  27.  
  28.  

with animal counts:
Code: QB64: [Select]
  1. REDIM dat$(0)
  2.     READ insert$
  3.     IF insert$ <> "EOD" THEN loadSort insert$, dat$() ELSE EXIT DO
  4. FOR i = LBOUND(dat$) TO UBOUND(dat$)
  5.     IF dat$(i) <> "" THEN
  6.         PRINT dat$(i)
  7.         IF concat$ = "" THEN
  8.             lastWord$ = dat$(i)
  9.             cntWord = 1
  10.             concat$ = dat$(i) + "#" + _TRIM$(STR$(cntWord))
  11.         ELSE
  12.             IF dat$(i) = lastWord$ THEN cntWord = cntWord + 1 ELSE cntWord = 1: lastWord$ = dat$(i)
  13.             concat$ = concat$ + ", " + dat$(i) + "#" + _TRIM$(STR$(cntWord))
  14.         END IF
  15.     END IF
  16. PRINT: PRINT concat$
  17.  
  18. DATA dog,cat,rabbit,frog,horse,dog,mouse,pig,cat,bat,cat,dog,bird,fish,cat,pig,dog,EOD
  19.  
  20. SUB loadSort (insertN AS STRING, dynArr() AS STRING) '  version 2020-06-07
  21.     'note this leaves dynArr(0) empty! so ubound of array is also number of items in list
  22.     DIM ub, j, k
  23.     ub = UBOUND(dynArr) + 1
  24.     REDIM _PRESERVE dynArr(LBOUND(dynArr) TO ub) AS STRING
  25.     FOR j = 1 TO ub - 1
  26.         IF insertN < dynArr(j) THEN '  GT to LT according to descending or ascending sort
  27.             FOR k = ub TO j + 1 STEP -1
  28.                 dynArr(k) = dynArr(k - 1)
  29.             NEXT
  30.             EXIT FOR
  31.         END IF
  32.     NEXT
  33.     dynArr(j) = insertN
  34.  
  35.  
« Last Edit: September 22, 2020, 07:47:03 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I can't decide which I like best...
« Reply #2 on: September 22, 2020, 08:16:27 pm »
Nice, Mark.

My way to add an array and place the results in alphabetical order would be as follows...

Code: QB64: [Select]
  1. WIDTH 144, 25
  2. j = 0
  3.     READ mydata$
  4.     IF mydata$ = "EOF" THEN EXIT DO
  5.     j = j + 1
  6. DIM myarray$(j)
  7.  
  8.     READ mydata$
  9.     IF mydata$ = "EOF" THEN EXIT DO
  10.     j = 1: occ% = 0: h = h + 1
  11.     dataconcat$ = mydata$ + "|" + dataconcat$
  12.     DO UNTIL j > LEN(dataconcat$)
  13.         IF INSTR(j, dataconcat$, mydata$ + "|") - j = 0 THEN occ% = occ% + 1
  14.         j = INSTR(j, dataconcat$, "|") + 1
  15.     LOOP
  16.     myarray$(h) = mydata$ + "#" + LTRIM$(STR$(occ%))
  17. dataconcat$ = ""
  18. FOR i = 1 TO h
  19.     FOR j = 1 TO h
  20.         IF i <> j THEN
  21.             IF myarray$(i) < myarray$(j) THEN SWAP myarray$(i), myarray$(j)
  22.         END IF
  23. NEXT j, i
  24. FOR i = 1 TO h
  25.     PRINT myarray$(i); ", ";
  26. PRINT "End Method #1."
  27.  
  28. DATA dog,cat,rabbit,frog,horse,dog,mouse,pig,cat,bat,cat,dog,bird,fish,cat,pig,dog
  29.  

Pete
« Last Edit: September 22, 2020, 08:17:44 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: I can't decide which I like best...
« Reply #3 on: September 22, 2020, 08:18:14 pm »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #4 on: September 22, 2020, 08:23:34 pm »
Hi Pete,

Line 5 is out of data even when I add the data line?

Never mind, you are looking for EOF, my End Of Data is EOD
« Last Edit: September 22, 2020, 08:29:10 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #5 on: September 22, 2020, 08:24:17 pm »
I'd use PowerShell to sort the data :)
https://stackoverflow.com/questions/32385611/sort-very-large-text-file-in-powershell

But powershell can't load the data from a bas file.


I'd use qsort for a more serious array size, something more than Pete's Ark.
« Last Edit: September 22, 2020, 08:26:34 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I can't decide which I like best...
« Reply #6 on: September 22, 2020, 08:27:04 pm »
PowerShell? Where's the fun in that? You'd probably use a rifle to hunt deer, too. Sigh. Just do what I do. Run them down and tear them apart with your bare hands. So you take a few hoof kicks to the head, so what? So you take a few hoof kicks to the head, so what? So you take a few hoof kicks to the head, so what?

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

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: I can't decide which I like best...
« Reply #7 on: September 22, 2020, 08:32:06 pm »
This response made me genuinely LOL.
You're not done when it works, you're done when it's right.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I can't decide which I like best...
« Reply #8 on: September 22, 2020, 08:33:38 pm »
Hi Pete,

Line 5 is out of data even when I add the data line?

Never mind, you are looking for EOF, my End Of Data is EOD

I've always used EOF from 20 years back, as: "End of File"  I like EOD, though. Anyway, you must have cut and pasted my code just before I edited in the data!


@Bill: Glad you liked it. Glad you liked it. Glad you liked it.

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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #9 on: September 22, 2020, 08:49:18 pm »
It's difficult to see a great difference between Method#1 and Method#2 but it's interesting you chose Method#1 for the sort demo.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I can't decide which I like best...
« Reply #10 on: September 22, 2020, 09:03:59 pm »
I chose #1, because I thought it was a bit cleaner of an approach, but I also liked #2, because it used MID$() to chop away at the concatenated string. My guess is with any code, it all comes down to which would perform the fastest in a speed test, maybe matched with the elegance of the code, itself. Still, if there was a bathing suit contest, I'd be more prone to watch.

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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #11 on: September 22, 2020, 10:03:30 pm »
I kinda like #1 because no extra variable k.

Are you going to try a speed test?

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I can't decide which I like best...
« Reply #12 on: September 22, 2020, 10:19:41 pm »
Not on my computers. Long gone are the days where I used to have state of the art systems. Right now, it mostly depends on what the OS is up to, how my programs respond. I've noticed with some of the speed tests we did around here in the past, my results were way to inconsistent to be of any use. Since I'm not a gamer, it doesn't bother me in the least but I do get a kick out of the responsiveness in high end systems. I mean from my days of coding with a tape recorder as a drive, and 4K of memory, we've come a long way.

Yeah, I liked being rid of that "k" as well.

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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I can't decide which I like best...
« Reply #13 on: September 22, 2020, 10:22:23 pm »
Make sure I am around when you try the swimsuit contest :)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I can't decide which I like best...
« Reply #14 on: September 22, 2020, 11:16:22 pm »
'K.
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/