Why does this not work?

BlitzMax Forums/BlitzMax Programming/Why does this not work?

TomToad(Posted 2012) [#1]
Local a:String = "Hello"
For Local x:Int = 0 Until a.length
	Print a[x]
	a[x] = 116
	Print a[x]
Next

Print a

Get error "Expression must be a variable."

I have tried replacing the 116 with "t", Asc("t"), int(116), and even tried defining Local b:int = 116, a[x] = b. Seems I remember you use to be able to alter a string this way.


ziggy(Posted 2012) [#2]
I think array access to string on BlitzMax is read only.


TomToad(Posted 2012) [#3]
So is there a way to alter just one character without resorting to slicing? Could be pretty slow when modifying large strings as slicing creates a new string each time.


Yasha(Posted 2012) [#4]
If you could modify a string in place, other variables also holding a reference to it would also be modified. Makes sense once you're used to the BlitzMax object system, but it violates the notion of strings as a value type rather than a by-reference type, which BlitzMax inherits from its predecessors (no matter how incorrect it was there too).

If necessary you can modify a string in-place by accessing it via a byte pointer? That would of course require hardcoding a specific knowledge of the implementation (find it in brl.mod/blitz.mod/blitz_string.h), and that way madness lies. If you really need to modify blocks of data in this way you might want to consider not using strings for the task.

Last edited 2012


ImaginaryHuman(Posted 2012) [#5]
varptr(a[x])[0]=116?


TomToad(Posted 2012) [#6]
This way seems to work, by using WStrings:
SuperStrict

Local s:String = "hello"
Local b:Short Ptr = s.towstring()

Print s

For Local x:Int = 0 Until s.length
	b[x] = 116+x
Next

s = String.fromwstring(b)
MemFree(b)
Print s



BlitzSupport(Posted 2012) [#7]

varptr(a[x])[0]=116?


Gesundheit.


Hummelpups(Posted 2012) [#8]
:-D