Home | Reviews | GUIpedia | Forum | Fun500


agumaTechnical BASIC String question
Okay, so first let me start off by explaining things. Strings are stored like this in BASIC: | Length | Char 1 | Char 2 | Char 3 |...and so on For example: | 5 | H | E | L | L | O | is how BASIC stores the string "hello". So my question is: if you have a string, say, "Hello World!", and take the byte or two before the string in memory (the place where the length of "hello world" is stored), and change the value to 5, then printed the variable that held the string (PRINT n$, if n$ = "Hello World!"), would it be "Hello"? Sorry if you didn't get that, I'm not very good at explaining things. :dry: EDIT: Here's a pseudocode example: String n = "Hello World!" Number f = 5 Put value in RAM (address of n - 1, f) 'Put f as the length of n Print n 'Will that be "Hello"?
2008-08-143:43 PM

ToddRe:Technical BASIC String question
I believe technically it will print the first 5 characters. But it could mess up the programming. BASIC typically stores strings in one section all together so changing the length could end up putting " World!" somewhere else in the program where it's not intended.
2008-08-144:10 PM

BrandonRe:Technical BASIC String question
Just use LEFT()!
2008-08-144:22 PM

ToddRe:Technical BASIC String question
LEFT would waste small amounts of processing time to retrieve a substring of the variable. I think Aguma is wondering if you can do it quicker by changing one variable. Aguma's way: [code] 1. Put "Hello World!" into memory 2. Set indexed length to 5 3. Print value [/code] Using LEFT: [code] 1. Put "Hello World!" into memory 2. Look for 5 left characters 3. Return 5 left characters 4. Print returned value [/code] Not a big difference except in microseconds.
2008-08-144:41 PM

agumaRe:Technical BASIC String question
I was just wondering what would happen, I don't really need it. Thanks though!
2008-08-144:52 PM

SonicBritRe:Technical BASIC String question
So yes you are correct that how basic stores it string, but theres a little more to it then just that DIM a AS STRING a = "Hello World!" PRINT a PRINT PEEK(VARPTR(a)) POKE VARPTR(a), 5 PRINT a PRINT PEEK(VARPTR(a)) That will so you what you are looking for but if you quit the program, or trying running it and changing it, you will notice you will get the error "string space corrupt" So it appears that it tracks it a bit more then just the first byte. String space is stored in dynamic memory (basically because strings can be resized and what not) so some extra tracking is involved, alas I dont know where, would be interesting to delve into though. However if you reset the value back POKE VARPTR(a),12 you will notice all appears to be ok, so depending on what you are trying to do that might be helpful.
2008-08-144:56 PM

BASIC Programming Help


2021 Brandon Cornell