Author Topic: TYPE vars white spaces  (Read 6955 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
TYPE vars white spaces
« on: January 13, 2019, 05:48:49 pm »
so working along on records...

using TYPE records, the following example

text as string * 300

while waiting for user input, I display the current (which is blank at the start),

so what it looks like at run time:

option 1: this option
option 2: this option
option 3: text




option 4: more options
option 5: more text..

notice the white space for option 3 (in this example)

when I use _TRIM$(text) it does not do what I would expect.

what kind of characters make up that space (NOTE: once I edit that line, all the white-space goes apply)
is there a way to display it while it is blank?


I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Marked as best answer by xra7en on January 13, 2019, 06:26:16 pm

FellippeHeitor

  • Guest
Re: TYPE vars white spaces
« Reply #1 on: January 13, 2019, 07:19:06 pm »
Try using LEFT$(text, INSTR(text, CHR$(0)) - 1).

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: TYPE vars white spaces
« Reply #2 on: January 13, 2019, 07:33:44 pm »
To explain further, those "white-spaces" are null characters represented as CHR$(0). They are added to strings less than their dimension limits, so text = "abc" would be recorded as abc with 277 CHR$(0) characters following the "c" in your DIM text as STRING * 300 example.

Fell's line of code will strip the null characters off your text variable.

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

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: TYPE vars white spaces
« Reply #3 on: January 13, 2019, 07:57:25 pm »
o duh, forgot about nul. I knew it was something along those lines. :-)

thanks guys
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: TYPE vars white spaces
« Reply #4 on: January 14, 2019, 01:32:14 am »
To explain further, those "white-spaces" are null characters represented as CHR$(0). They are added to strings less than their dimension limits, so text = "abc" would be recorded as abc with 277 CHR$(0) characters following the "c" in your DIM text as STRING * 300 example.

Fell's line of code will strip the null characters off your text variable.

Pete

Just half of the truth, usually a regular assignment such as text = "abc" does fill the remaining 277 (or whatever) bytes with spaces on fixed length strings, but if you're just replacing chars in the form MID$(text, 1, 3) = "abc", then the other bytes remain uninitialized, hence CHR$(0).

That's why I usually do a xxx = "" on any fixed length strings first anywhere in my program's init procedure, it will make it all spaces and then you can also use MID$ for char replacement without leaving the other parts uninitialized, as they are already spaces after the first assignment.

BTW - xra7en, that's also the reason that it works, once you have edited/assigned it once.

My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: TYPE vars white spaces
« Reply #5 on: January 14, 2019, 04:29:47 pm »
most of that I figured. the chr$(0) slipped my mind that is WAS NOT a character, and because of that, I thought the _TRIM$() would take care of it.
also, was taking the easy rout. instead of making a whole new varible(s). I just used the existing type record to input example

Code: QB64: [Select]
  1. type tRoom
  2. pname as string * 40
  3. desc as string *300
  4.  
  5. dim room as troom
  6.  
  7. 'show current stuff
  8. print "name = ";room.pname
  9. print "desc = ";room.desc '<--- oops lots of spaces the first time (lazy or efficient?)
  10.  
  11.  
  12. input room.pname
  13. input room.desc
  14.  

after user adds the data, all looks good.
 it is more aesthetics than anything.

However, recently, I just decided to add the dummy vars to use and then when done, made the record vars equal the dummy vars.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: TYPE vars white spaces
« Reply #6 on: January 15, 2019, 11:22:40 am »
I am not expert for qb64 but i know something about basic.
First as far as i know white-space or blank char -> " "... is chr(32) not chr(0)
chr(0) is empty string char -> ""
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: TYPE vars white spaces
« Reply #7 on: January 15, 2019, 01:26:03 pm »
CHR$(0) can show up in strings through odd events. Here is an example...

Code: QB64: [Select]
  1. DIM x AS STRING * 20
  2. DIM a AS STRING * 10
  3. OPEN "tmp.tmp" FOR OUTPUT AS #1: CLOSE #1
  4. OPEN "tmp.tmp" FOR RANDOM AS #1 LEN = 20
  5. a = "123"
  6. PUT #1, 1, a$
  7. GET #1, 1, x
  8.  
  9. FOR i = 1 TO 20
  10.     PRINT ASC(MID$(x, i, 1)), MID$(x, i, 1), x
  11. PRINT STRING$(10, CHR$(0)); "|"
  12.  

Now if you coded a AS STRING * 20, it would show all chr$(32) spaces. You can really screw with the output if you get rid of the DIM a and change a = "123" to a$ = "123". QB64 must love it, because it returns a leading heart. CHR$(3)

CHR$(0) does take up space, btw. It's just an invisible character, like CHR$(32) and CHR$(255).

PRINT STRING$(10, CHR$(0));"|"

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

FellippeHeitor

  • Guest
Re: TYPE vars white spaces
« Reply #8 on: January 15, 2019, 01:46:47 pm »
CHR$(3) showing up when you use a variable length string instead of a fixed length one is merely a "header" indicating the size of the string. Making a$ = "four" or whatever other four-character string will yield a diamond, as that's chr$(4).

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: TYPE vars white spaces
« Reply #9 on: January 15, 2019, 02:44:22 pm »
And what happens if my string is 200,000 characters long? Does the "heart" explode?

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

FellippeHeitor

  • Guest
Re: TYPE vars white spaces
« Reply #10 on: January 15, 2019, 03:26:04 pm »
From internal/c/libqb.cpp:
Quote
//put2 adds a 2-4 byte length descriptor to the data
//(used to PUT variable length strings in RANDOM mode)
void sub_put2(int32 i,int64 offset,void *element,int32 passed){

If the length of the string is less than 32767, it's stored using an INTEGER (2 bytes). If greater than that, it's stored using a LONG (4 bytes).
« Last Edit: January 15, 2019, 03:34:17 pm by FellippeHeitor »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: TYPE vars white spaces
« Reply #11 on: January 15, 2019, 04:07:52 pm »
Interesting.

When I first started using RA files, I did not take the time to study TYPE or fixed length strings. I don't think I even knew what a fixed length string was. So I did get that RA files had to have a way to access records, and if the string was not the correct length, it would cause a bad record error. I found a way around all of this by adding +2 to the LEN statement that was set to the length of the longest string I wanted stored...

Code: QB64: [Select]
  1. REDIM a$(10), b$(10)
  2. OPEN "tmp.tmp" FOR OUTPUT AS #1: CLOSE #1
  3. a$(1) = "This is an example"
  4. a$(2) = "of using a random access format"
  5. a$(3) = "that can handle strings of"
  6. a$(4) = "varying lengths."
  7. FOR i = 1 TO 4
  8.     IF LEN(a$(i)) > max THEN max = LEN(a$(i))
  9. WIDTH 80, 42
  10. OPEN "tmp.tmp" FOR RANDOM AS #1 LEN = max + 2
  11. FOR i = 1 TO 4
  12.     PUT #1, i, a$(i)
  13. FOR i = 1 TO 4
  14.     GET #1, i, b$(i)
  15.  
  16. FOR h = 1 TO 4
  17.     PRINT b$(h)
  18. FOR h = 1 TO 4
  19.     CLS
  20.     FOR i = 1 TO LEN(b$(h))
  21.         PRINT ASC(MID$(b$(h), i, 1)), MID$(b$(h), i, 1)
  22.     NEXT
  23.     SLEEP
  24.  

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

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: TYPE vars white spaces
« Reply #12 on: January 15, 2019, 04:22:30 pm »
When I first started using RA files, I did not take the time to study TYPE or fixed length strings. I don't think I even knew what a fixed length string was. So I did get that RA files had to have a way to access records, and if the string was not the correct length, it would cause a bad record error. I found a way around all of this by adding +2 to the LEN statement that was set to the length of the longest string I wanted stored...

If the length of the strings may end up being longer than 32766 characters, yuo'd want to make that statement + 4.  ;)

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: TYPE vars white spaces
« Reply #13 on: January 15, 2019, 06:42:51 pm »
My strings seldom exceed those levels... unless I'm cursing out Democrats.

Pete ;)

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

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: TYPE vars white spaces
« Reply #14 on: January 17, 2019, 10:06:47 pm »
Interesting.

When I first started using RA files, I did not take the time to study TYPE or fixed length strings. I don't think I even knew what a fixed length string was. So I did get that RA files had to have a way to access records, and if the string was not the correct length, it would cause a bad record error. I found a way around all of this by adding +2 to the LEN statement that was set to the length of the longest string I wanted stored...


OK, I'm old school, what is RA files? to me that reads Remote Access (bbs days) files.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!