Author Topic: lining up numbers in columns  (Read 6464 times)

0 Members and 1 Guest are viewing this topic.

Offline Babyboomerboy

  • Newbie
  • Posts: 9
    • View Profile
lining up numbers in columns
« on: March 29, 2019, 07:52:09 pm »
Got a small problem. I have a for next routine that prints numbers 5 columns wide and 8 rows down. When it prints out, if one of the numbers is 2 digits, it moves the rest of the numbers on that row off line with the other numbers in that column. I remember several years ago I fixed this problem but I don’t remember how I did it. Any help would be appreciated.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: lining up numbers in columns
« Reply #1 on: March 29, 2019, 08:11:49 pm »
Easiest solution is to probably just use LOCATE.

FOR I = 1 TO 8
   LOCATE I, 1: PRINT I
   LOCATE I, 10: PRINT I * 2
   LOCATE I, 20: PRINT I * 3
   LOCATE I, 30: PRINT I * 4
   LOCATE I, 40: PRINT I * 5
NEXT

If you just want to print without setting the vertical position, just use LOCATE ,

FOR I = 1 TO 8
   LOCATE , 1: PRINT I;
   LOCATE , 10: PRINT I * 2;
   LOCATE , 20: PRINT I * 3;
   LOCATE , 30: PRINT I * 4;
   LOCATE , 40: PRINT I * 5
NEXT

Note: Notice the semi-colons which don’t cause the PRINT to move to a new row, until we want it to at the end.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: lining up numbers in columns
« Reply #2 on: March 29, 2019, 10:13:19 pm »
Do us a favor. Post the code for the for/next loop. I don't understand how a 2-digit number is a problem in a 5-digit column.

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

Offline Babyboomerboy

  • Newbie
  • Posts: 9
    • View Profile
Re: lining up numbers in columns
« Reply #3 on: March 29, 2019, 11:15:02 pm »
I am not very good at explaining my problem. When I run the code I might get 7 3 5 4
                                                                                                                   1 8 12 6
The 12 moved the 6 to the right of the column with the 4.
The 1 lines up under the 7 and the8 lines up under the 3
Here is the code

CLS
LOCATE 20, 15
PRINT
PRINT TAB(14); "        OFX,ETX,STX,FFX,   BTIME,BTIMEA"
FOR TTP = 1 TO 8
    PRINT TAB(15); "DOG"; TTP; "-"; OFX(TTP); " "; ETX(TTP); " "; STX(TTP); " "; FFX(TTP); "     "; BTIMEA%(TTP); " "; BTIMEA(TTP)
NEXT TTP
PRINT " "
PRINT TAB(5); "                  OP, EP, SP, FP,  PTOTAL"
FOR TTP = 1 TO 8

    PRINT TAB(15); "DOG"; TTP; " "; OPOINT(TTP); " "; EPOINT(TTP); " "; SPOINT(TTP); " "; FPOINT(TTP); "       "; POINTTOTALA(TTP)
NEXT TTP
INPUT H

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lining up numbers in columns
« Reply #4 on: March 30, 2019, 12:11:33 am »
Code: QB64: [Select]
  1. FOR row = 1 TO 20
  2.     FOR col = 1 TO 10
  3.         r$ = STR$(INT((RND * 10 - 5) ^ INT(12 * RND - 6))) 'some wild and crazy numbers
  4.         PRINT RIGHT$(SPACE$(8) + r$, 8); '<<<<<<<<<<<<<<<<<< space out numbers on row
  5.     NEXT
  6.     PRINT
  7.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: lining up numbers in columns
« Reply #5 on: March 30, 2019, 12:43:32 am »
Here is an example with your first row/column code, and the code modified with LOCATE and LEN() LTRIM$ and STR$() keywords.

Let me know if this is the kind of alignment you wanted, and if so, I'd be happy to explain how the code woks.

Pete

Code: QB64: [Select]
  1. WIDTH 80, 42: _SCREENMOVE 0, 0
  2. LOCATE 20, 15
  3. PRINT TAB(14); "        OFX,ETX,STX,FFX,   BTIME,BTIMEA"
  4. FOR TTP = 1 TO 8
  5.     OFX(TTP) = 7 + TTP: ETX(TTP) = 3 + TTP: STX(TTP) = 5 + TTP: FFX(TTP) = 4 + TTP: BTIMEA%(TTP) = 1 + TTP: BTIMEA(TTP) = 12 + TTP ' Make some test values.
  6.     PRINT TAB(15); "DOG"; TTP; "-"; OFX(TTP); " "; ETX(TTP); " "; STX(TTP); " "; FFX(TTP); "     "; BTIMEA%(TTP); " "; BTIMEA(TTP)
  7. NEXT TTP
  8. PRINT: LOCATE , 15: PRINT "Aligned Columns...": PRINT
  9. FOR TTP = 1 TO 8
  10.     LOCATE , 15: PRINT ; "DOG"; TTP;: LOCATE , 21: PRINT "-";
  11.     LOCATE , 25 - LEN(LTRIM$(STR$(OFX(TTP)))): PRINT LTRIM$(STR$(OFX(TTP)));
  12.     LOCATE , 29 - LEN(LTRIM$(STR$(ETX(TTP)))): PRINT LTRIM$(STR$(ETX(TTP)));
  13.     LOCATE , 33 - LEN(LTRIM$(STR$(STX(TTP)))): PRINT LTRIM$(STR$(STX(TTP)));
  14.     LOCATE , 37 - LEN(LTRIM$(STR$(FFX(TTP)))): PRINT LTRIM$(STR$(FFX(TTP)));
  15.     LOCATE , 44 - LEN(LTRIM$(STR$(BTIMEA%(TTP)))): PRINT LTRIM$(STR$(BTIMEA%(TTP)));
  16.     LOCATE , 49 - LEN(LTRIM$(STR$(BTIMEA(TTP)))): PRINT LTRIM$(STR$(BTIMEA(TTP)))
  17. NEXT TTP
  18.  
  19. PRINT " "
  20. PRINT TAB(5); "                  OP, EP, SP, FP,  PTOTAL"
  21. FOR TTP = 1 TO 8
  22.  
  23.     PRINT TAB(15); "DOG"; TTP; " "; OPOINT(TTP); " "; EPOINT(TTP); " "; SPOINT(TTP); " "; FPOINT(TTP); "       "; POINTTOTALA(TTP)
  24. NEXT TTP
  25.  
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Babyboomerboy

  • Newbie
  • Posts: 9
    • View Profile
Re: lining up numbers in columns
« Reply #6 on: April 01, 2019, 09:33:27 am »
Thank you SMcNeil, bplus and Pete. I was able to use the code that Pete gave the best for my situation. I moved the columns a little wider and everyone lined up perfect.  You said you could explain how the code works and I would appreciate knowing this as I have learned to code all by myself through trial and error, lots of errors. Thanks again to all

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lining up numbers in columns
« Reply #7 on: April 01, 2019, 01:30:58 pm »
Maybe exposure to more methods would help?

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. 'set column starts for left alignment or column ends for right alignment
  4. DIM cTabs(1 TO 4)
  5. cTabs(1) = 5
  6. cTabs(2) = 15
  7. cTabs(3) = 25
  8. cTabs(4) = 35
  9.  
  10.  
  11. '  this method gives you left alignment at set column starts
  12. PRINT "1234567890123456789012345678901234567890"
  13. FOR row = 2 TO 11
  14.     LOCATE row, cTabs(1): PRINT -INT(RND * 10)
  15.     LOCATE row, cTabs(2): PRINT -INT(RND * 100)
  16.     LOCATE row, cTabs(3): PRINT -INT(RND * 1000)
  17.     LOCATE row, cTabs(4): PRINT -INT(RND * 10000)
  18.  
  19.  
  20. ' to get right alignment on columns which is desired for columns of numbers
  21.  
  22. 'subtract off the LEN() of the item you are about to print when you use LOCATE
  23. ' the cTabs become the right side of the cols
  24.  
  25. LOCATE 13, 1: PRINT "1234567890123456789012345678901234567890"
  26. FOR row = 14 TO 25
  27.  
  28.     n$ = STR$(INT(RND * 10)) 'next number to print
  29.  
  30.     'subtract it's LEN from cTabs(col)
  31.     LOCATE row, cTabs(1) - LEN(n$): PRINT n$
  32.  
  33.     'repeat for all numbers in row
  34.     n$ = STR$(INT(RND * 100))
  35.     LOCATE row, cTabs(2) - LEN(n$): PRINT n$
  36.     n$ = STR$(INT(RND * 1000))
  37.     LOCATE row, cTabs(3) - LEN(n$): PRINT n$
  38.     n$ = STR$(INT(RND * 10000))
  39.     LOCATE row, cTabs(4) - LEN(n$): PRINT n$
  40.  
  41.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: lining up numbers in columns
« Reply #8 on: April 01, 2019, 02:08:26 pm »
Sure...

STR$() Converts a numeric value to a string.
LTRIM$() stands for Left Trim, which means it trims off any spaces to the left side of a string.

Strings are letters like, A,b,c, and other characters like #, *, etc. Now strings can also be numbers, but here's a big difference. Try the code below...

Code: QB64: [Select]
  1. a = 1
  2. a$ = "1"
  3. PRINT "|"; a; "|"
  4. PRINT "|"; a$; "|"
  5. PRINT "|"; STR$(a); "|"
  6. PRINT "|"; LTRIM$(STR$(a)); "|"
  7.  

So printing a number as a numeric variable results in a space in front of the number, and a space in back of the number. Now when we use STR$() to represent the numeric variable as a string, it gets rid of the trailing space, but notice it still keeps that leading space. That space is reserved for a negative sign. See code below...

Code: QB64: [Select]
  1. a = -1
  2. a$ = "-1"
  3. PRINT "|"; a; "|"
  4. PRINT "|"; a$; "|"
  5. PRINT "|"; STR$(a); "|"
  6.  

So if a negative sign was present, it wouldn't need to be "Left Trimmed" but you could still use LTRIM$(STR$(a)) if you want to. I always use it that way, as most of my work contains negative and positive numbers, so I just want one simple way to do it all. So LTRIM$(STR$(a)) makes the 1 just take up one space, instead of 3 spaces, and that is critical to aligning text.

Next, LOCATE...

LOCATE has two variables, ROW,  COLUMN. So why did I leave the ROW part out and write LOCATE , 15 in the code? Well, omitting the ROW variable tells QB64 to locate on the current row. So if I'm on the 10th row,  LOCATE , 15 keeps me on the 10th row, and starts the first text writing space at COLUMN 15. So, no need to write it as LOCATE 10, 15. It's neat typing saver!

Finally, the LEN() statement. LEN() measures the length of your string. Not a big deal if they are all the same length, but let's say the first variable is one space like "7" and in the row below it, the next variable is: "53" So the first variable has LEN("1") = 1 character in length, and the second LEN("53") would be 2-characters long. If we wanted to line up the variables, so they all end at column 9 we would need...

Code: QB64: [Select]
  1. LOCATE , 10 - LEN(LTRIM$(STR$(7))): PRINT "7"
  2. LOCATE , 10 - LEN(LTRIM$(STR$(53))): PRINT "53"

So the first statement starts at the current row at column 10 - length of the number 7, which is 10 - 1 or 9. It then prints 1 at column 9. Notice the PRINT statement is not followed by a semi-colon as in PRINT; so just PRINT alone tells QB64 to print and move down to the next row. A semi-colon added would tell it to stay on the same row. So since we omitted a semi-colon, we printed 7 and we are now on the next row, below. The second statement locates on that row at column 10 - 2, because our next number, 53, has a length of 2-characters. We use LTRIM$(STR$(53)) to get rid of the leading and trailing spaces. So the 5 of the 53 gets printed at column 8 (10 - 2) and the 3 ends up at column 9, the same column the number 7 is printed in, in the row above.

I hope this makes sense. If not, just ask.

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: lining up numbers in columns
« Reply #9 on: April 01, 2019, 04:45:08 pm »
Dang! I keep forgetting about the space on the right, left from STR$(number) (including my last snippet above, which explains why the numbers weren't finishing exactly on the cTab() value).

Best to TRIM$(STR$(number)) with a newer version of QB64

« Last Edit: April 01, 2019, 07:10:55 pm by bplus »

FellippeHeitor

  • Guest
Re: lining up numbers in columns
« Reply #10 on: April 01, 2019, 05:01:05 pm »
_TRIM it is :-)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lining up numbers in columns
« Reply #11 on: April 01, 2019, 06:26:14 pm »
Thanks Fellippe,

Where is that right space when you are looking for it?
Code: QB64: [Select]
  1. _TITLE "Test _TRIM$"
  2. n = 42.
  3. ns$ = STR$(42)
  4. nts$ = _TRIM$(ns$)
  5. PRINT LEN(ns$), "*"; ns$; "*"
  6. PRINT LEN(nts$), "*"; nts$; "*"
  7.  

Code: QB64: [Select]
  1. _TITLE "Test _TRIM$"
  2. n = 42.
  3. ns$ = STR$(n)
  4. nts$ = _TRIM$(ns$)
  5. PRINT LEN(ns$), "*"; ns$; "*"
  6. PRINT LEN(nts$), "*"; nts$; "*"
« Last Edit: April 01, 2019, 06:27:38 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: lining up numbers in columns
« Reply #12 on: April 01, 2019, 06:43:04 pm »
And remember simple little TAB...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline RadioHacktive

  • Newbie
  • Posts: 1
    • View Profile
Re: lining up numbers in columns
« Reply #13 on: April 01, 2019, 06:54:51 pm »
"Print using" does not work? http://qb64.org/wiki/PRINT_USING

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lining up numbers in columns
« Reply #14 on: April 01, 2019, 07:11:44 pm »
Wait, where did I get this idea of there being a space on the right of a STR$(number)?

To get a number to right align on column x it is: LOCATE ,  x - LEN(STR$(number)) + 1
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. 'set column starts for left alignment or column ends for right alignment
  4. DIM cTabs(1 TO 4)
  5. cTabs(1) = 5
  6. cTabs(2) = 15
  7. cTabs(3) = 25
  8. cTabs(4) = 35
  9.  
  10.  
  11. ' to get right alignment on columns which is desired for columns of numbers
  12.  
  13. 'subtract off the LEN() of the item you are about to print when you use LOCATE
  14. ' the cTabs become the right side of the cols
  15.  
  16. LOCATE 13, 1: PRINT "1234567890123456789012345678901234567890"
  17. FOR row = 14 TO 25
  18.  
  19.     n$ = STR$(INT(RND * 10)) 'next number to print
  20.  
  21.     'subtract it's LEN from cTabs(col)
  22.     LOCATE row, cTabs(1) - LEN(n$) + 1: PRINT n$
  23.  
  24.     'repeat for all numbers in row
  25.     n$ = STR$(INT(RND * 100))
  26.     LOCATE row, cTabs(2) - LEN(n$) + 1: PRINT n$
  27.     n$ = STR$(INT(RND * 1000))
  28.     LOCATE row, cTabs(3) - LEN(n$) + 1: PRINT n$
  29.     n$ = STR$(INT(RND * 10000))
  30.     LOCATE row, cTabs(4) - LEN(n$) + 1: PRINT n$
  31.  


"Print using" does not work? http://qb64.org/wiki/PRINT_USING

Save that for REAL Numbers! ;-))