[futurebasic] LEN(s) vs s[0] (Was A new trick?)

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2005 : Group Archive : Group : All Groups

From: Stu Cram <stu@...>
Date: Sun, 31 Jul 2005 17:42:54 -0600
Scott's example below works because s[0] has the length code for the 
string (0-255 characters). This is how FutureBasic stores string 
variables, following Pascal conventions. (FWIW-Early Mac OS were 
written and defined in Pascal.)

I would suggest though that the newer s[0] nottion may not be clear to 
some programmers if they don't realize the internal structure of Pascal 
strings within FutureBasic.

So, here's another version, with the standard LEN(s) function rather 
than s[0]. Also, as per my structured programming course 30+ years ago, 
each result is calculated first and stored in a variable before using 
those variables in the output/print command. I've found that very good 
advice in the long run.

	s = "Meet Klara Voyant - She's all knowing!"
	TEXT _Courier, 12
	FOR i = 1 TO LEN( s )
	  asciiNum = s[i]              ' or MID$( s, i, 1 ) in older/other 
	  theChar = chr$( asciiNum )
	  PRINT USING "#####"; asciiNum ; "    "; theChar

Using s[0] is handy though for specialized routines that may need to 
change the length of string as these examples show...

a)		s[0] = 0   ' this clears the string by making its length byte zero.

b)		IF s[0] > maxLen THEN s[0] = maxLen  ' restricts s to a certain # 
of characters

Just a few thoughts to share..
-Stu Cram, Regina, Sask. Canada

On Jul 31, 2005, at 4:43 PM, Scott Spencer wrote this example to 
display ASCII codes and characters of a string s:

> for i = 1 to s[0]
> print "ASCII #: " s[i], "Char: " + chr$(s[i])
> next