Page 1 of 1

RIGHT$ problem

Posted: Mon Dec 06, 2004 11:46 pm
by Dr. D'nar
RIGHT$ doesn't seem to be working propley...

Code: Select all

INPUT "File to read from: ", infile$
INPUT "File to output to: ", outfile$
OPEN infile$ FOR INPUT AS #1
OPEN outfile$ FOR APPEND AS #2

DO
temp$ = ""
sep! = 0
ans$ = ""
que$ = ""
INPUT #1, temp$
cnt! = cnt! + 1
sep! = INSTR(temp$, "|")
ans$ = LEFT$(temp$, sep! - 1)
que$ = RIGHT$(temp$, sep! + 1)
PRINT sep! 'not really need; for debugging only
PRINT ans$ 'not really need; for debugging only
PRINT que$ 'not really need; for debugging only
PRINT #2, sep! 'not really need; for debugging only
PRINT #2, que$ + "?" + "*" + ans$
LOOP UNTIL EOF(1) = -1
CLOSE #2
CLOSE #1
PRINT "All finished!"
Input file...
answer|question
hijklmnop|abcdefg
zsdx|adsd
67890|12345
efghgg|abcdff

Output file...
9
r|question?*answer
16
hijklmnop|abcdefg?*hijklmnop
7
sdx|adsd?*zsdx
6
0|12345?*67890
7
g|abcdff?*efghgg

Posted: Tue Dec 07, 2004 4:59 am
by Dr_Davenstein
You might want to use MID$ instead... Is this what you want?

Code: Select all

CLS
Temp$ = "Mid$ is|a better solution."

Sep% = INSTR(Temp$, "|")
ans$ = MID$(Temp$, 1, Sep% - 1)
que$ = MID$(Temp$, Sep% + 1)
PRINT Sep% 'not really need; for debugging only
PRINT ans$ 'not really need; for debugging only
PRINT que$ 'not really need; for debugging only

PRINT
PRINT "All finished!"
Output:
8
Mid$ is
a better solution.

All finished!

Posted: Fri Dec 10, 2004 2:00 am
by Dr. D'nar
Yes!

(But why doesn't RIGHT$ work?)

Posted: Sun Dec 12, 2004 8:55 pm
by Dr. D'nar
Okay here's the new code:

Code: Select all

INPUT "File to read from: ", infile$
INPUT "File to output to: ", outfile$
OPEN infile$ FOR INPUT AS #1
OPEN outfile$ FOR APPEND AS #2

DO
'Temp$ = ""
'Sep! = 0
'ans$ = ""
'que$ = ""
INPUT #1, Temp$
cnt! = cnt! + 1
Sep! = INSTR(Temp$, "|")
ans$ = MID$(Temp$, 1, Sep% - 1)
que$ = MID$(Temp$, Sep% + 1)
PRINT Sep!
PRINT ans$
PRINT que$
'PRINT #2, sep!
PRINT #2, que$ + "?" + "*" + ans$
LOOP UNTIL EOF(1) = -1
CLOSE #2
CLOSE #1
PRINT "All finished!"
And of course it refuses to work. Illagle fuction call. Right. Illagle error call.

RIGHT$ problem

Posted: Wed Dec 15, 2004 6:22 am
by Ralph
Hmm... I like to find one answer at a time, so:

If your first reading is:
"answer|question "
then the INSTR function should return 7, not 9, as "|" is the 7th character in the string "answer|question". So, how about clearing up this discrepancy first?
With 9 as the answer, "r|question" is correct. Could it be that the reading is, actually, " answer|question", that is, "answer|question", with two spaces in front?

I tried your program with "answer|question" as the string, and I got the correct answers, 7, "answer" and "question".

Posted: Thu Dec 16, 2004 4:07 am
by Dr_Davenstein
Try converting all the numeric variables to integer(%) first. I don't have much time right now... Mid$ works correctly for me though.