ombre
New Member
Posts: 12
|
string
Mar 25, 2020 10:06:04 GMT -5
Post by ombre on Mar 25, 2020 10:06:04 GMT -5
I want to insert a space in a string where there is none.
Here's my code:
n$="MauriceRichard"
long=len(n$)
dim w$(long)
for z=1 to long
w$(z)=mid$(n$,z,1)
next z
for z=long to 1 step -1 'Starting from the end
if w$(z)>=chr$(65) and w$(z) <= chr$(90) then print w$(z):exit for
next z
Why does the if clause not work?
|
|
|
string
Mar 25, 2020 10:37:11 GMT -5
Post by Chris Iverson on Mar 25, 2020 10:37:11 GMT -5
The colon.
The colon is used to join two command on separate lines into one. The command that's getting paired is NOT the 'print' command(so the exit for would also execute under the condition), it's the IF command itself.
Your code looks to the compiler like this:
if w$(z)>=chr$(65) and w$(z) <= chr$(90) then print w$(z) exit for
See the issue? The for loop is exited before the first time through the loop completes.
If you want multiple commands in a condition, you need to break it out into a block.
if w$(z)>=chr$(65) and w$(z) <= chr$(90) then print w$(z) exit for end if
Also, moving this from 'bugs'.
|
|
|
string
Mar 25, 2020 10:39:54 GMT -5
Post by David Drake on Mar 25, 2020 10:39:54 GMT -5
Perhaps this will accomplish what you want?
if asc(w$(z))>=65 and asc(w$(z)) <= 90 then
|
|
|
string
Mar 25, 2020 12:58:27 GMT -5
Post by tsh73 on Mar 25, 2020 12:58:27 GMT -5
Chris,
I believe you wrong here. Pretty sure actually. Colon after THEN relates to THEN clause making it compound, it always worked that way.
ombre,
so how exactly you are trying to do that? If you throw EXIT FOR, it will print all letters. In you want to add empty line, do PRINT or PRINT "" But then should it be printed? David code looks like an answer So let's write letter ALWAYS and add a line AFTER CAPITAL letter
for z=long to 1 step -1 'Starting from the end print w$(z) 'letter always if asc(w$(z))>=65 and asc(w$(z)) <= 90 then print 'after capital letter next z
Is it what you wanted?
As to why initial condition doesn't work as expected -
if w$(z)>=chr$(65) and w$(z) <= chr$(90) then - well it is because LB has it's own sorting order on chars. It makes me LITERALLY MAD each time I stumble on it. (yes, that and lack of unary minus.) I really don't know any other language that does that.
|
|
|
Post by Chris Iverson on Mar 25, 2020 13:17:56 GMT -5
Ah, you're right. I misread the condition, and I don't usually use compound statements. I especially wouldn't use compound statements in a conditional block. I've seen compound statements in other languages behave like I said, instead of how LB uses them.
My apologies.
|
|
ombre
New Member
Posts: 12
|
string
Mar 25, 2020 14:06:44 GMT -5
Post by ombre on Mar 25, 2020 14:06:44 GMT -5
Thanks David Drake and tsh73.
if asc(w$(z))>=65 and asc(w$(z)) <=90 then print w$(z):exit for
Works perfectly
|
|