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

0 Members and 1 Guest are viewing this topic.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Trouble understanding MID$( )
« Reply #30 on: November 03, 2021, 01:47:48 pm »
I'm telling you, my strings aren't working right. I don't use graphics, I do everything in strings. Check out my CryptoGram.bas to see what I'm talking about. I posted it a while ago. I don't know if it's still on the boards. I'm pretty good with string manipulation. I admonish there's a possibility there was an error in my code but at this point I either don't know everything I need to (when a QB64 function takes a 'byte' as a variable for example), my computer has a personal issue with me or there's an internal error in QB64. I rewrote that portion of the code a little differently but the files the LINE INPUTs come from has a lot of text and also times. The idea was to make sure that the only thing to code for was at a specific place in the input line and that it was definitely a time at that location. If something in the future that gives me significant difficulty, I'll post it.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Trouble understanding MID$( )
« Reply #31 on: November 03, 2021, 02:15:09 pm »
Here's my first attempt at writing a little code to extract time from a string; see if it'd be suitable for your needs:

Code: QB64: [Select]
  1. test$ = "The time was " + Time$ + "."
  2. test$ = test$ + "  The time is now " + Time$ + "."
  3. Print test$
  4.     tyme$ = GetTime$(test$, p)
  5.     If tyme$ <> "" Then Print tyme$: p = p + 1
  6. Loop Until tyme$ = ""
  7.  
  8. test$ = "My ratio is 1:2.  " + test$ + "  And my updated time is " + Time$
  9. p = 0
  10. Print test$
  11.     tyme$ = GetTime$(test$, p)
  12.     If tyme$ <> "" Then Print tyme$: p = p + 1
  13. Loop Until tyme$ = ""
  14.  
  15. Function GetTime$ (text$, position)
  16.     FirstColon = InStr(position, text$, ":")
  17.     If FirstColon Then 'If we have one colon then
  18.         SecondColon = InStr(FirstColon + 1, text$, ":")
  19.         If SecondColon - FirstColon <> 3 Then 'it's not a proper time.
  20.             position = FirstColon + 1
  21.             GetTime$ = GetTime$(text$, position) 'check to see if we have a proper time AFTER the false occurance.
  22.             Exit Function
  23.         End If
  24.         GetTime$ = Mid$(text$, FirstColon - 2, 8)
  25.         position = SecondColon
  26.     End If

What this does is look for the essential two middle colons of our time and then grab the proper 8 digits from those reference points.   Something like 1:12 wouldn't work, as it'd be at least expecting to see the time in a HH:MM:SS format.

This doesn't look for, or grab "AM" or "PM" references, as it's basically expecting TIME$ type formats, and those are in 24-hour times. 

There's no real error checking to see if we generate false positives with this method (such as grabbing AT:14:XP), as I'm keeping the basic process as simple as possible for illustration's sake.  This type of thing doesn't have to be difficult at all, if you just stick to the basics of what you're expecting with it.

  [ You are not allowed to view this attachment ]  
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 #32 on: November 03, 2021, 03:10:19 pm »
@Jaze Simple tests for your computer and your QB64 copy:

Use Mid$ to print the two ll's in "Hello World"
Print Mid$("Hello World", 3, 2)

Use Mid$ to print the word World from "Hello World"
Print Mid$("Hello World", 7, 5)
or just
Print Mid$("Hello World", 7)

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: Trouble understanding MID$( )
« Reply #33 on: November 03, 2021, 03:11:00 pm »
I find it easier to just split the string based on a delimiter, then look through the results (an array that is populated by my strSplit$ FUNCTION) for the section that I need.

Here is my STRSPLIT$ FUNCTION, which populates an array named Query (declared as STRING SHARED). A string (qString$) and a delimiter (Delim$) is passed to the function.

The thing is, MID$ works properly. I use this to parse results from mySQL query results.

Code: QB64: [Select]
  1. FUNCTION StrSplit$ (qString$, Delim$)
  2. '-----------------------------------------------------------------------------
  3. ' *** Split a STRING based on provided Delimiter
  4. ' ***      qString$ and Delim$ are passed to this function
  5. '
  6.         lenstr = LEN(qString$)
  7.         idx = 1
  8.         startpos = 1
  9.         DO
  10.                 findpos% = INSTR(findpos% + 1, qString$, Delim$) ' find another occurance
  11.                 IF findpos% THEN
  12.                         nbrbytes = findpos% - startpos
  13.                         Query(idx) = MID$(qString$, startpos, nbrbytes)
  14.                         startpos = findpos% + 1
  15.                         idx = idx + 1
  16.                 END IF
  17.         LOOP UNTIL findpos% = 0
  18.         nbrbytes = lenstr - startpos + 1
  19.         Query(idx) = MID$(qString$, startpos, nbrbytes)
  20.        
  21.         StrSplit$ = "0"
  22.  
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #34 on: November 03, 2021, 03:20:59 pm »
Here's how I would convert a Time$ format (hh:mm:ss)
Code: QB64: [Select]
  1. T$ = Time$
  2. hrs = Val(Mid$(T$, 1, 2))
  3. mins = Val(Mid$(T$, 4, 2))
  4. secs = Val(Mid$(T$, 7, 2))
  5. If hrs > 11 Then m$ = " PM" Else m$ = " AM"
  6. If hrs > 12 Then hrs = hrs - 12
  7. Print hrs; mins; secs; m$
  8.  
  9.  

Now you're ready to build an analog clock! ;-))

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #35 on: November 03, 2021, 03:42:52 pm »
I'm telling you, my strings aren't working right. I don't use graphics, I do everything in strings. Check out my CryptoGram.bas to see what I'm talking about. I posted it a while ago. I don't know if it's still on the boards. I'm pretty good with string manipulation. I admonish there's a possibility there was an error in my code but at this point I either don't know everything I need to (when a QB64 function takes a 'byte' as a variable for example), my computer has a personal issue with me or there's an internal error in QB64. I rewrote that portion of the code a little differently but the files the LINE INPUTs come from has a lot of text and also times. The idea was to make sure that the only thing to code for was at a specific place in the input line and that it was definitely a time at that location. If something in the future that gives me significant difficulty, I'll post it.

I dug up that old post, CryptoGram.bas, and commented on your use of Mid$ there.
https://www.qb64.org/forum/index.php?topic=4166.msg137774#msg137774

Offline eekee

  • Newbie
  • Posts: 3
    • View Profile
Re: Trouble understanding MID$( )
« Reply #36 on: November 07, 2021, 02:39:58 am »
This looks very much like the sort of errors I used to make for years. I felt like I was getting on all right, but I commonly mis-remembered syntax or started doing something differently and didn't notice. I often couldn't see anything wrong even when it was something very simple. Despite this, I could still type mostly coherent English and my programming errors tended to only show in one specific thing. Typically for me, it was regular expressions.

As such, I respectfully suggest the OP get more fresh air. Carbon dioxide -- the stuff we breathe out -- builds up fast and has a surprisingly stong affect on brain function. I forget the exact figures, but it's easy to build up enough carbon dioxide to halve brain function. Bedrooms have been known to build up even more than that. Ventilation is essential. Sadly, it's sometimes impossible to get enough ventilation; some entire valleys develop unacceptably high carbon dioxide levels on still days. It must also be noted that microbes which consume organic detritus (dirt) also produce carbon dioxide.

As to why this is showing up with MID$ specifically, I recall it being the hardest function to understand when I was little. I don't know why, it just was. I hated it for a while.

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Trouble understanding MID$( )
« Reply #37 on: November 07, 2021, 04:28:02 am »
what ,what and what
i must say that i am surprised with MID$
it is such a simple and elegant string function which return subString ...right?

mark says :

mid$( t$, len) ..yes it is ok but not for everyone....it should be standard

MID$ ( target_string, start_Pos, Len_Of_substring)
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trouble understanding MID$( )
« Reply #38 on: November 07, 2021, 08:53:43 am »
what ,what and what
i must say that i am surprised with MID$
it is such a simple and elegant string function which return subString ...right?

mark says :

mid$( t$, len) ..yes it is ok but not for everyone....it should be standard

MID$ ( target_string, start_Pos, Len_Of_substring)

Nope, nope and nope! :) This is incorrect:
Quote
mark says :

mid$( t$, len) ..yes it is ok but not for everyone....it should be standard

For section of string that starts at Start_Position and ends with the end of Text$ use:
MID$( Text$, Start_Position )  ' < and let IDE correct spacing
no LEN or lengths are needed for 2 parameter MID$ function.

Well I hope we don't have everyone confused now ;-))

MID$ Statement (Sub) is really nice too! It will increase speed over concatenation.