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

0 Members and 1 Guest are viewing this topic.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Trouble understanding MID$( )
« on: November 01, 2021, 03:58:25 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)
  6.  
the result I get for timeString$ is " 9:30 AM N-the lobby between 10:13 AM-N   "

wtf?

am I missing something? I've had lots of problems with MID$(), especially with INSTR(). MID$() often requires me to fudge the numbers but still works consistently after doing so. Any suggestions?

FellippeHeitor

  • Guest
Re: Trouble understanding MID$( )
« Reply #1 on: November 01, 2021, 04:12:37 pm »
It looks like a matter of adjusting expectations. What did you expect to happen?

Offline SMcNeill

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

Looks to me like things line up proper.  The 34th character is the space before 9, and 42 characters (including it) gives you the rest of the text.  What's wrong with the output?

Are you looking for endTime to be 42-34, or only 8 characters total?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #3 on: November 01, 2021, 04:25:29 pm »
Perhaps you want this:
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, 8)
  6. Print timeString$
  7.  
  8.  
  9.  

Mid$(string$, startPosition, ForLengthOf)

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Trouble understanding MID$( )
« Reply #4 on: November 01, 2021, 04:29:22 pm »
I've had so many bugs with MID$ that I probably didn't check the numbers this time. anyway, I wrote my own just now. duh!

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Trouble understanding MID$( )
« Reply #5 on: November 01, 2021, 04:38:18 pm »
no. I had it correct. All I did was change "MID$" to the function I wrote "Middle$" and the exact same code functioned the way I intended. Thankfully I haven't had any difficulty with LEFT$ or RIGHT$ or I wouldn't have been able to write the function. Thanks anyway

FellippeHeitor

  • Guest
Re: Trouble understanding MID$( )
« Reply #6 on: November 01, 2021, 06:08:44 pm »
Aaaaand we never learned what he intended to do after all.

Bottom line: MID$ not buggy.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #7 on: November 01, 2021, 06:23:52 pm »
I'll bet real money he intended this:
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$ = Middle$(lineToPrint$, startTime, endTime)
  6. Print timeString$
  7.  
  8. Function Middle$ (S$, Start_Position&, End_Position&)
  9.     Middle$ = Mid$(S$, Start_Position&, End_Position& - Start_Position&)
  10.  
  11.  
  12.  

It's very logical for a noobie to think like that.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #8 on: November 01, 2021, 06:29:46 pm »
But how'd he do it with Left$ and Right$?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Trouble understanding MID$( )
« Reply #9 on: November 01, 2021, 07:18:18 pm »
But how'd he do it with Left$ and Right$?

Like so:

Code: QB64: [Select]
  1. startTime = 34
  2. endTime = 42 - startTime
  3. lineToPrint$ = "|                                 9:30 AM N-the lobby between 10:13 AM-N   |"
  4.  
  5. timeString$ = Mid$(lineToPrint$, startTime, endTime)
  6.  
  7. Print Middle$(lineToPrint$, 34, 42)
  8. Print timeString$
  9.  
  10. Function Middle$ (text$, start, finish)
  11.     m$ = Left$(text$, finish - 1)
  12.     m$ = Right$(m$, finish - start)
  13.     Middle$ = m$
  14.  

Note the -1 is my interpretation that he wants to end AT that position and not actually include it.  If I'm wrong, remove that shift.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #10 on: November 01, 2021, 07:32:25 pm »
Yeah the -1 is picked up in the next use of finish.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #11 on: November 01, 2021, 08:22:29 pm »
You know I was looking at this and looking at 34 and 42 and then looking at this and... WTH?
Code: QB64: [Select]
  1. lineToPrint$ = "123456789012345678901234567890123423456789012345678901234567890123456789012"
  2. lineToPrint$ = "|                                 9:30 AM N-the lobby between 10:13 AM-N   |"
  3.  


Oh! you started recounting at 34! Here's a real character ruler:
Code: QB64: [Select]
  1. 'lineToPrint$ = "123456789012345678901234567890123423456789012345678901234567890123456789012"
  2. 'lineToPrint$ = "|                                 9:30 AM N-the lobby between 10:13 AM-N   |"
  3. 'lineToPrint$ = "123456789012345678901234567890123456789012345678901234567890123456789012"
  4. 'lineToPrint$ = "         1         2         3   ^     4 ^       5         6
  5.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Trouble understanding MID$( )
« Reply #12 on: November 02, 2021, 12:09:20 am »
Not only is MID$ not buggy, it is also very forgiving. This does not apply to the example posted, but it's worth mentioning other languages will error out if the range given is not within the length of the string. I just thought I'd parse that along.

I was also wondering if the OP was thinking the third argument would cut the string at the 42nd character, instead of include 42 characters following the value of the second argument, 34. I mean if he was looking of a result of: _930 AM he needed to subtract the start time from the end time as in: timeString$ = Mid$(lineToPrint$, startTime, endTime - startTime)

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Trouble understanding MID$( )
« Reply #13 on: November 02, 2021, 12:21:30 am »
You know I was looking at this and looking at 34 and 42 and then looking at this and... WTH?
Code: QB64: [Select]
  1. lineToPrint$ = "1234567890123456789012345678901234"
  2. lineToPrint$ = "                                 123456789012345678901234567890123456789012"
  3. lineToPrint$ = "|                                 9:30 AM N-the lobby between 10:13 AM-N   |"
  4.  


Oh! you started recounting at 34!

Maybe the above highlights it better.  That position with the last 4 (for 34) also where we start counting a 1 out of our 42 characters that we want to retrieve.  (Which is why we go from 4 to 2 on the second round of numbering, as the digit after is the 2nd digit we're keeping...)

Maybe I should've broke it down into two parts like this originally, but I wanted to illustrate that the second started where the first ended, *while including that end point*.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Trouble understanding MID$( )
« Reply #14 on: November 03, 2021, 12:00:25 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
« Last Edit: November 03, 2021, 12:46:45 am by odin »