Author Topic: Trouble understanding MID$( )  (Read 9860 times)

0 Members and 1 Guest are viewing this topic.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Trouble understanding MID$( )
« Reply #15 on: November 03, 2021, 12:14:06 am »
QB64 doesn't handle the ":" character properly with string manipulation

Code: QB64: [Select]
  1. checkForTime$ = "6:30 AM"
  2. PRINT "|" + MID$(checkForTime$, 2, 1) + "|"
prints out the empty string i.e. || with nothing in between

No, it prints
Code: [Select]
|:|

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Trouble understanding MID$( )
« Reply #16 on: November 03, 2021, 12:20:26 am »
Have you went into libqb.cpp and made custom changes to MID$?  This is the second time you've reported it being buggy when things are working entirely as expected.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Trouble understanding MID$( )
« Reply #17 on: November 03, 2021, 12:38:16 am »
What the heck, Jaze. Please do follow up on the discussion. Previous thread was even renamed, since MID$() is working exactly as expected for everyone but you.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Trouble understanding MID$( )
« Reply #18 on: November 03, 2021, 11:15:07 am »
I wrote the code differently and don't have a copy of it, but it is definitely not working as it should all the time. When I run into another error with strings (which I inevitably will), I'll post the exact code, the results I got and the results I expected. Basically I was trying to determine if a string was a time. The only character that got the correct results was the first digit. Sorry for not being more specific, but like I said, I'll definitely write a more thorough post if (when) it happens again

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Trouble understanding MID$( )
« Reply #19 on: November 03, 2021, 11:23:44 am »
I was  able to undo to get the exact code. The string assigned to checkForTime$ is "6:30 AM". It is read from a text file using a LINE INPUT and then passed to the SUB with this code
Code: QB64: [Select]
  1.     IF LEN(checkForTime$) = 7 THEN
  2.         d1$ = Middle$(checkForTime$, 1, 1): digit1 = VAL(d1$)
  3.         colon$ = Middle$(checkForTime$, 2, 1)
  4.         d$2 = Middle$(checkForTime$, 3, 1): digit2 = VAL(d2$)
  5.         d3$ = Middle$(checkForTime$, 4, 1): digit3 = VAL(d3$)
  6.         aSpace$ = Middle$(checkForTime$, 5, 1)
  7.         aORp$ = Middle$(checkForTime$, 6, 1)
  8.         m$ = Middle$(checkForTime$, 7, 1)
  9.         COLOR 15, 0: LOCATE 1, 1: PRINT "checkForTime$ = |" + checkForTime$ + "|"
  10.         LOCATE 2, 1: PRINT d1$ + colon$ + d2$ + d3$ + aSpace$ + m$
  11.         t$ = P$(1)'wait for a key press
  12.         IF colon$ = ":" AND aSpace$ = " " THEN
  13.             IF (digit1 >= 1 AND digit1 <= 9) AND ((digit2 >= 1 AND digit2 <= 5) OR d2$ = "0") THEN 'AND
  14.                 IF ((digit3 >= 1 AND digit3 <= 9) OR d3$ = "0") THEN 'AND
  15.                     IF (aORp$ = "A" OR aORp$ = "P") AND m$ = "M" THEN
  16.                         timeThere = TRUE
  17.                     END IF
  18.                 END IF
  19.             END IF
  20.         END IF
checkForTime$ prints out at the top correctly
The line below it prints out "6" and nothing else

FellippeHeitor

  • Guest
Re: Trouble understanding MID$( )
« Reply #20 on: November 03, 2021, 11:27:40 am »
The code you provided above doesn't use MID$(), and we have no idea what your custom Middle$() function is doing.

FellippeHeitor

  • Guest
Re: Trouble understanding MID$( )
« Reply #21 on: November 03, 2021, 11:30:34 am »
to simply detect whether there's a colon in there, check:

Code: QB64: [Select]
  1. IF INSTR(checkForTime$, ":") > 0 THEN PRINT "There's a colon in this string"

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Trouble understanding MID$( )
« Reply #22 on: November 03, 2021, 11:38:03 am »
Sorry. I've run into a lot of bugs with strings so I wrote my own MID$ and named it Middle$().

Here's the code for it
Code: QB64: [Select]
  1. FUNCTION Middle$ (text$, startPosition, endPosition)
  2.     rtn$ = ""
  3.     newText$ = text$
  4.     FOR count = 1 TO startPosition - 1
  5.         newText$ = RIGHT$(newText$, LEN(newText$) - 1)
  6.     NEXT count
  7.     FOR count = startPosition TO endPosition
  8.         rtn$ = rtn$ + LEFT$(newText$, 1)
  9.         newText$ = RIGHT$(newText$, LEN(newText$) - 1)
  10.     NEXT count
  11.     Middle$ = rtn$
  12.  

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Trouble understanding MID$( )
« Reply #23 on: November 03, 2021, 11:40:42 am »
The code you provided above doesn't use MID$(), and we have no idea what your custom Middle$() function is doing.
@FellippeHeitor
I think he might be using the MIDDLE$ function Bplus or Steve made earlier in this thread. Still no mention on just what he is expecting.

@Jaze
Just what are you trying to accomplish? It is totally possible MID$ may not be the function you are looking for to do the work you need, but without knowing just what you want for output I don't think anybody is going to be able to address your exact issue.
We need to know what you need done to know just why your not getting it from the code.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #24 on: November 03, 2021, 11:41:31 am »
I was  able to undo to get the exact code. The string assigned to checkForTime$ is "6:30 AM". It is read from a text file using a LINE INPUT and then passed to the SUB with this code
Code: QB64: [Select]
  1.     IF LEN(checkForTime$) = 7 THEN
  2.         d1$ = Middle$(checkForTime$, 1, 1): digit1 = VAL(d1$)
  3.         colon$ = Middle$(checkForTime$, 2, 1)
  4.         d$2 = Middle$(checkForTime$, 3, 1): digit2 = VAL(d2$)
  5.         d3$ = Middle$(checkForTime$, 4, 1): digit3 = VAL(d3$)
  6.         aSpace$ = Middle$(checkForTime$, 5, 1)
  7.         aORp$ = Middle$(checkForTime$, 6, 1)
  8.         m$ = Middle$(checkForTime$, 7, 1)
  9.         COLOR 15, 0: LOCATE 1, 1: PRINT "checkForTime$ = |" + checkForTime$ + "|"
  10.         LOCATE 2, 1: PRINT d1$ + colon$ + d2$ + d3$ + aSpace$ + m$
  11.         t$ = P$(1)'wait for a key press
  12.         IF colon$ = ":" AND aSpace$ = " " THEN
  13.             IF (digit1 >= 1 AND digit1 <= 9) AND ((digit2 >= 1 AND digit2 <= 5) OR d2$ = "0") THEN 'AND
  14.                 IF ((digit3 >= 1 AND digit3 <= 9) OR d3$ = "0") THEN 'AND
  15.                     IF (aORp$ = "A" OR aORp$ = "P") AND m$ = "M" THEN
  16.                         timeThere = TRUE
  17.                     END IF
  18.                 END IF
  19.             END IF
  20.         END IF
checkForTime$ prints out at the top correctly
The line below it prints out "6" and nothing else

This line is screwed up, I am curious why no error?
Code: QB64: [Select]
  1. d$2 = Middle$(checkForTime$, 3, 1): digit2 = VAL(d2$)
  2.  

This almost works or looks like something when MID$( is substituted back in for Middle$(
Code: QB64: [Select]
  1. checkforTime$ = "6:30 AM"
  2.  
  3. If Len(checkforTime$) = 7 Then
  4.     d1$ = Mid$(checkforTime$, 1, 1): digit1 = Val(d1$)
  5.     colon$ = Mid$(checkforTime$, 2, 1)
  6.     d2$ = Mid$(checkforTime$, 3, 1): digit2 = Val(d2$) ' <<< fix d$2 to d2$
  7.     d3$ = Mid$(checkforTime$, 4, 1): digit3 = Val(d3$)
  8.     aSpace$ = Mid$(checkforTime$, 5, 1)
  9.     aORp$ = Mid$(checkforTime$, 6, 1)
  10.     m$ = Mid$(checkforTime$, 7, 1)
  11.     Color 15, 0: Locate 1, 1: Print "checkForTime$ = |" + checkforTime$ + "|"
  12.     Locate 2, 1: Print d1$ + colon$ + d2$ + d3$ + aSpace$ + m$
  13.     t$ = P$(1) 'wait for a key press
  14.     If colon$ = ":" And aSpace$ = " " Then
  15.         If (digit1 >= 1 And digit1 <= 9) And ((digit2 >= 1 And digit2 <= 5) Or d2$ = "0") Then 'AND
  16.             If ((digit3 >= 1 And digit3 <= 9) Or d3$ = "0") Then 'AND
  17.                 If (aORp$ = "A" Or aORp$ = "P") And m$ = "M" Then
  18.                     timeThere = TRUE
  19.                 End If
  20.             End If
  21.         End If
  22.     End If
  23.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #25 on: November 03, 2021, 11:51:41 am »
All fixed! ??
Code: QB64: [Select]
  1. checkforTime$ = "6:30 AM" '<<< watch out! some times have 2 digits at start like now 11:55 AM
  2. TRUE = -1
  3. If Len(checkforTime$) = 7 Then
  4.     d1$ = Mid$(checkforTime$, 1, 1): digit1 = Val(d1$)
  5.     colon$ = Mid$(checkforTime$, 2, 1)
  6.     d2$ = Mid$(checkforTime$, 3, 1): digit2 = Val(d2$) ' <<< fix d$2 to d2$
  7.     d3$ = Mid$(checkforTime$, 4, 1): digit3 = Val(d3$)
  8.     aSpace$ = Mid$(checkforTime$, 5, 1)
  9.     aORp$ = Mid$(checkforTime$, 6, 1)
  10.     m$ = Mid$(checkforTime$, 7, 1)
  11.     Color 15, 0: Locate 1, 1: Print "checkForTime$ = |" + checkforTime$ + "|"
  12.     'Locate 2, 1: Print d1$ + colon$ + d2$ + d3$ + aSpace$ + m$ ' <<<<<<<<<<<<<<<<<<<<<<<<<<<< missing aORp$ I think!
  13.     Locate 2, 1: Print d1$ + colon$ + d2$ + d3$ + aSpace$ + aORp$ + m$
  14.     t$ = P$(1) 'wait for a key press
  15.     If colon$ = ":" And aSpace$ = " " Then
  16.         If (digit1 >= 1 And digit1 <= 9) And ((digit2 >= 1 And digit2 <= 5) Or d2$ = "0") Then 'AND
  17.             If ((digit3 >= 1 And digit3 <= 9) Or d3$ = "0") Then 'AND
  18.                 If (aORp$ = "A" Or aORp$ = "P") And m$ = "M" Then
  19.                     timeThere = TRUE
  20.                     Print timeThere
  21.                 End If
  22.             End If
  23.         End If
  24.     End If
  25.  

FellippeHeitor

  • Guest
Re: Trouble understanding MID$( )
« Reply #26 on: November 03, 2021, 12:15:50 pm »
Quote
This line is screwed up, I am curious why no error?

d2$ is the same as DIM d2 AS STRING
d$2 is the same as DIM d AS STRING * 2

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #27 on: November 03, 2021, 12:21:33 pm »
d2$ is the same as DIM d2 AS STRING
d$2 is the same as DIM d AS STRING * 2

Wow, did not know that! Thanks

PS I am sure that was not Jaze intention, it was typo I bet.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #28 on: November 03, 2021, 12:44:28 pm »
I like a Middle$ routine, it's a nice one for toolbox and using Rtn$ is excellent substitute for Function name for QB64 v2.0+

Here is another way to do Middle$ without Left$, Right$, or that peculiar Mid$ ;-))
Code: QB64: [Select]
  1. s$ = "Middle$"
  2. For i = 1 To Len(s$)
  3.     Print Middle$(s$, 1, i),
  4.     Print Middle$(s$, i, Len(s$))
  5.  
  6. Function Middle$ (text$, startPosition, endPosition)
  7.     For i = startPosition To endPosition
  8.         rtn$ = rtn$ + Chr$(Asc(text$, i))
  9.     Next
  10.     Middle$ = rtn$
  11.  
  12.  
  13.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Trouble understanding MID$( )
« Reply #29 on: November 03, 2021, 01:25:47 pm »
Code: QB64: [Select]
  1. startTime = 34
  2. endTime = 42
  3. lineToPrint$ = "|                                 9:30 AM N-the lobby between 10:13 AM-N   |"
  4.  
  5.  timeString$ = MID$(lineToPrint$, startTime, endTime)

Code: [Select]
    IF LEN(checkForTime$) = 7 THEN
        d1$ = Middle$(checkForTime$, 1, 1): digit1 = VAL(d1$)
        colon$ = Middle$(checkForTime$, 2, 1)

From these two code snippets. it's obvious that Jaze has expectations that his code will read his mind.  In the first, he wants MID$ to work with a (string, start point, end point) syntax  In the second, he's expecting it to work with a (string, start point, number of characters) syntax. 

You can't have it both ways! 

MID$ itself works with the (string, start, number of bytes) syntax.  MIDDLE$, which you wrote yourself, works with the (string, start point, end point) syntax.  The two commands *AREN'T* interchangeable and you need to use the proper one to meet your expectations.

 
Since you posted code with using your custom MIDDLE$ sub, I'd imagine that the line in question should actually look like one of the two options below:
Code: QB64: [Select]
  1.         colon$ = Middle$(checkForTime$, 2, 2)
  2.         colon$ = Middle$(checkForTime$, 2, 3)

The proper one of those two variations will depend on exactly how you wrote your MIDDLE$ function.  If it's start point, end point, then 2, 2 will be fine.   If it's startpoint, endpoint - startpoint, then the 3,2 is probably what you need.   Regardless, you can't start at position 2 and then try to get the proper information back from that point to position 1 as the end point.  I doubt you wrote your code to read in reverse, so all you're going to get from 2, 1 with MIDDLE$ is a blank string "".  (Even if you did write it to read in reverse, you'd just get a "1" and not the ":" as you're reading from the opposite direction...)

Syntax on our commands doesn't change.  We can't expect it to conform to what we WANT it to be at our whimsey.  Write it consistently and you won't have issues with it.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!