Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
INKEY$ doesn't work with fixed length strings
#12
(07-30-2024, 03:53 PM)SMcNeill Wrote: Wouldn't _KEYHIT simply be easier for this?

INKEY$ returns a 1 or 2-byte string value for user input.  _KEYHIT returns that *exact* same value, except in Integer form, rather than string.  You can easily convert between the two with the MKI$ and CVI commands.   It just seems to me if you're always going to be dealing with 2-byte strings, that it'd probably just be more efficient overall to just use the integer values.  There's always extra overhead when dealing with strings, and they're always slower than integers to process and handle, so I'd honestly just _KEYHIT over INKEY$.
You are correct, I mentioned this at the beginning of the post. 

I'm not so worried about the functionality of INKEY$ as much as the utility of fixed length strings. I just wished they behaved like normal strings.

For example, using LEN returns size of memory allocated not the last character in the string. Perhaps that is how LEN works for variable length strings under the hood, but the end user sees it as the last character. I'm not saying this wrong, but It would be nice to have a function that returns the actual length of string.

Concatenation is problematic, because it puts the two blocks of memory together and calls it a day, instead finding the actual last character of the first string and then adding on to that.

Another oddity is fixed length strings are initialized with CHR$(0), but as soon as you assign it a string, it fills the void with spaces. If it were left as CHR$(0) then it would be easier to roll our own LEN function.

INKEY$ is just a demonstration of the limitations that fixed strings have.

Here is a sample of what I'm talking about.

Code: (Select All)
TYPE tt
a AS STRING * 8
b AS STRING * 8
END TYPE

DIM t AS tt
PRINT "t.a = "; t.a
PRINT "t.b = "; t.b
PRINT
PRINT "LEN() before being assigned:"; LEN(t.a), LEN(t.b)
PRINT "Actual Length:"; sLen(t.a), sLen(t.b)
PRINT
PRINT "This is what it is filled with."
FOR i = 1 TO LEN(t.a)
PRINT ASC(MID$(t.a, i, 1)); " ";
NEXT
PRINT

t.a = "01234"
t.b = "56789"
PRINT
PRINT "t.a = "; t.a
PRINT "t.b = "; t.b
PRINT

PRINT "LEN() after being assigned:"; LEN(t.a), LEN(t.b)
PRINT "Actual Length:"; sLen(t.a), sLen(t.b)
PRINT
PRINT "This is what it is filled with."
FOR i = 1 TO LEN(t.a)
PRINT ASC(MID$(t.a, i, 1)); " ";
NEXT
PRINT
PRINT
PRINT "Concatenation"
PRINT t.a + t.b
PRINT
PRINT "Modified Concantenation"
PRINT LEFT$(t.a, sLen(t.a)) + t.b



FUNCTION sLen (s AS STRING)
DIM l AS LONG: l = LEN(s)
' How is this supposed to know if the last space was assigned or put there by QB64
DO WHILE (ASC(MID$(s, l, 1)) = 32 OR ASC(MID$(s, l, 1)) = 0) AND l > 1
  l = l - 1
LOOP
' Again, how is this supposed to know if the last space was assigned or put there by QB64
IF (ASC(MID$(s, l, 1)) = 32 OR ASC(MID$(s, l, 1)) = 0) THEN l = l - 1
sLen = l
END FUNCTION
Reply


Messages In This Thread
RE: INKEY$ doesn't work with fixed length strings - by justsomeguy - 07-30-2024, 06:18 PM



Users browsing this thread: 1 Guest(s)