Posts: 300
Threads: 30
Joined: Nov 2023
Reputation:
3
Hi Marcus,
I was tinkering with string manipulation and came across a bit of a 'poser' in regards to the mid() command.
Here is a short example....
Code: ' Open a window and enable double buffering.
set window "Test", 800, 632, false
set redraw off
visible a = "ABCDEF"
visible b
visible c
' display the letters "B" to "E" inclusive...
b = mid(a, 1, 4)
pln b
' This works fine...
' But, when I try to make the third letter "D"
' an "X"...
' mid(b, 3, 1) = "X"
' pln b
' I get a syntax error on line 17 during compile
'(remove the "remarks" from lines 17 and 18
' to see what I mean...)
system "pause"
Defining a string then assigning a variable to a "portion" of the string, using mid(), works fine...
But, trying to replace a character, using mid() causes the compilation to fail with a syntax error.
There is no rush on this one... Whenever you are able... Present situation understood... We will "keep an eye on the store" during your break...
J
Logic is the beginning of wisdom.
Posts: 190
Threads: 20
Joined: Nov 2023
Reputation:
0
Perhaps like this one ... ?
Code: 'Console Mode
a = "ABCDEF"
pln "a = "+a
pln
'display the letters "B" to "E" inclusive...
b = mid(a,1,4)
pln "MID(a,1,4) = "+b
'replace D with X
pln "REPLACE D with X = "+replace(b,"D","X",2)
pln
system "pause"
Posts: 300
Threads: 30
Joined: Nov 2023
Reputation:
3
Cool... Worked like a charm... Thank you!
Logic is the beginning of wisdom.
Posts: 190
Threads: 20
Joined: Nov 2023
Reputation:
0
(06-11-2024, 12:25 PM)johnno56 Wrote: Cool... Worked like a charm... Thank you!
The mid() function may be used as collision detection.
Let's try this one !
Code: '=====================================================
'PACMAN in N7
'Control Key :
'- movement WASD
'- exit ESC
'
'Reference :
'Tutorial on QBasic Game Design by Andre van Wyk
'http://petesqbsite.com/sections/tutorials/tutorials/basicgd.htm
'
'Quick Tips on qbasic to N7
'-locate y,x - set caret x,y
'-print - write
'-mid$() - mid
'======================================================
#win32
set window "Pacman",80,60,false,10
set redraw off
'color definition
black = [0,0,0]
white = [255,255,255]
yellow = [255,255,0]
'initial Pacman's position
x = 40
y = 10
'maze
maze = []
maze[0] = "########"
maze[1] = "# # #"
maze[2] = "# # #"
maze[3] = "# ## # #"
maze[4] = "# # #"
maze[5] = "########"
do
set color black ; cls
'maze
set color white
for j=0 to 7
for i = 0 to 5
set caret j*10,i*10
write mid(maze[i],j)
next
next
'remember Pacman's position
posX = x
posY = y
'control
if keydown(KEY_W) and y>1 then y=y-10
if keydown(KEY_S) and y<60 then y=y+10
if keydown(KEY_A) and x>1 then x=x-10
if keydown(KEY_D) and x<80 then x=x+10
'collision detection Pacman and Maze
if mid(maze[y/10],x/10)="#" then
x = posX
y = posY
endif
'Pacman
set color yellow
set caret x,y ; write "C"
redraw
fwait 10
until keydown(KEY_ESCAPE,true)
Posts: 300
Threads: 30
Joined: Nov 2023
Reputation:
3
Cool... Thanks for the demo... What? No ghosts? lol
Chunk text. Very retro...
Logic is the beginning of wisdom.
Posts: 316
Threads: 39
Joined: Nov 2023
Reputation:
3
Sorry, you can't really modify strings, only construct new ones.
Posts: 300
Threads: 30
Joined: Nov 2023
Reputation:
3
hmm... Interesting response... I am receiving mixed messages... On one hand, "can't" (cannot) implies a "No"... But, "can't really" implies a measure of doubt and conveys a possible interpretation of... "No. But I dare you to try". I enjoy a good puzzle, but when it comes to coding, I find myself at the shallow end of the "smart" pool... lol
Not a problem... Time for new train of thought... The only problem is that I do not have a ticket... Boom Boom.... lol
Thank you for responding...
Logic is the beginning of wisdom.
Posts: 316
Threads: 39
Joined: Nov 2023
Reputation:
3
06-15-2024, 08:23 AM
(This post was last modified: 06-15-2024, 08:24 AM by Marcus.)
(06-14-2024, 08:34 PM)johnno56 Wrote: hmm... Interesting response... I am receiving mixed messages... On one hand, "can't" (cannot) implies a "No"... But, "can't really" implies a measure of doubt and conveys a possible interpretation of... "No. But I dare you to try". I enjoy a good puzzle, but when it comes to coding, I find myself at the shallow end of the "smart" pool... lol
Not a problem... Time for new train of thought... The only problem is that I do not have a ticket... Boom Boom.... lol
Thank you for responding... ![Wink Wink](https://www.naalaa.com/forum/images/smilies/wink.png)
Haha
Strings are like numbers. You can't change a string without getting a new string. That's why mid/left/right/replace all return new strings. It doesn't really matter. There's no simple way of replacing a character at a certain index in a string with another character. If you know what character it is, you could probably use 'replace' to get a new string:
Code: foo = "ABCDEF"
pln "foo = " + foo
foo = replace(foo, "C", "Z")
pln "foo = " + foo
system "pause"
Output:
foo = ABCDEF
foo = ABZDEF
|