Author Topic: FUNCTION Replace$() - roll your own and share it  (Read 4039 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
Re: FUNCTION Replace$() - roll your own and share it
« Reply #15 on: July 27, 2020, 08:56:01 pm »
hey but you have found a bug of OPTION _EXPLICIT
... it lets's to use Subprograms without an END at the end of the main!

Come again?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FUNCTION Replace$() - roll your own and share it
« Reply #16 on: July 27, 2020, 08:57:57 pm »
@bplus
great the stone math....
hey but you have found a bug of OPTION _EXPLICIT
... it lets's to use Subprograms without an END at the end of the main!
For my hobbist eyes it is horrible (I think because there is a voice of Pascal/TurboPascal into my mind)
would Pascal let you execute the main without closing it with END. ?
Would C let you execute the main without closing it with } ?
Also QB45 does this thing, but it has Subroutines first of the main and after the declaration of themselves.

@TempodiBasic  sorry to hear of your dis-ease (a joke) ;-))

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: FUNCTION Replace$() - roll your own and share it
« Reply #17 on: July 27, 2020, 09:30:17 pm »
what disease?
1. the normalization MUST of maniac man
2.  the voice hallucination  of schizophrenic man

@FellippeHeitor
this is scaring you, is it? :-)
Programming isn't difficult, only it's  consuming time and coffee

FellippeHeitor

  • Guest
Re: FUNCTION Replace$() - roll your own and share it
« Reply #18 on: July 27, 2020, 09:34:04 pm »
Just trying to understand if you're being cheeky or if you actually found a bug with option _explicit.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FUNCTION Replace$() - roll your own and share it
« Reply #19 on: July 27, 2020, 09:41:56 pm »
Quote
what disease?

Quote
For my hobbist eyes it is horrible (I think because there is a voice of Pascal/TurboPascal into my mind)
would Pascal let you execute the main without closing it with END. ?

Pascal's disease of seeing (or is it NOT seeing?), the horror of no end in sight! Pascal actually suffered this! Did you know?


Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: FUNCTION Replace$() - roll your own and share it
« Reply #20 on: July 28, 2020, 07:15:48 am »
@FellippeHeitor May I ask why this post is in Discussions and not in Programs?  It's got lots of code stuff that members have created.  Posts in Discussions will never be searched for by a Librarian (well this one, anyway) for inclusion into the Library, even though a general resource based post might be applicable.
(I know that Fellippe wanted this to be a discussion, but nevertheless).

FellippeHeitor

  • Guest
Re: FUNCTION Replace$() - roll your own and share it
« Reply #21 on: July 28, 2020, 07:29:51 am »
Were discussing methods and none of this is a definitive product yet.

Marked as best answer by on July 04, 2024, 08:52:23 pm

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: FUNCTION Replace$() - roll your own and share it
« Reply #22 on: July 28, 2020, 09:15:22 am »
  • Undo Best Answer
  • Programs
    This section is for (i) completed programs, or (ii) works in progress.

    Only a minor point.

    Offline SpriggsySpriggs

    • Forum Resident
    • Posts: 1145
    • Larger than life
      • View Profile
      • GitHub
    Re: FUNCTION Replace$() - roll your own and share it
    « Reply #23 on: July 28, 2020, 09:23:47 am »
    Programs
    This section is for (i) completed programs, or (ii) works in progress.

    I'm with Fellippe on this one. It would be better to flesh everything out here first before moving something to programs.
    Shuwatch!

    Offline phred

    • Newbie
    • Posts: 11
      • View Profile
    Re: FUNCTION Replace$() - roll your own and share it
    « Reply #24 on: July 28, 2020, 10:35:10 am »
    Quote
    Why strings for numeric input?

    Well, I'm pretty sure there may have been a reason but I can't remember it now, or consistency; something like that. Pretty sure.

    phred

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: FUNCTION Replace$() - roll your own and share it
    « Reply #25 on: July 28, 2020, 11:54:01 am »
    After some more considerations I have once again updated the plainest most straight forward replace function with this:

    Beeps can be just as annoying as the program halting (like div by 0) if there is a problem causing argument.
    I like MID$() because it's no problem when the arguments don't fit the source string.

    Also now only make one call for LEN(replace$) and LEN(new$), a little more efficient for really long strings with lots of replacing.

    Code: QB64: [Select]
    1. _TITLE "strReplace$ test" 'b+ 2019-12-28     check to see that new l's don't get more replacements
    2. ' mod 2020-07-26 check for string lengths on source$ and replace$ but replacement new$ can be ""
    3. ' mod 2020-07-27 don't set strReplace$ = s$ because could cause unintented recursion.
    4. ' mod 2020-07-28 no beep, just return s$ if s$ or replace$ are 0 length
    5. '                also make variable "constants for lengths of replace and new to avoid multiple calls to LEN()
    6.  
    7. PRINT strReplace$("hello all", "ll", "l")
    8. PRINT strReplace$("hello all", "l", "lllll")
    9. PRINT strReplace$("lots of luck", "l", "lllll")
    10. PRINT strReplace$("Hello all", "ll", "") 'test a couple of deletions
    11. PRINT strReplace$("l Hello all", "l", "") '
    12. PRINT strReplace$("", "l", "no source") 'test error conditions
    13. PRINT strReplace$("source no replace", "", "new") 'test error conditions
    14. PRINT strReplace$("", "", "no source or replace") 'test error conditions
    15.  
    16. FUNCTION strReplace$ (s$, replace$, new$) 'case sensitive  2020-07-28 version
    17.     DIM p AS LONG, sCopy$, LR AS INTEGER, lNew AS INTEGER
    18.     IF LEN(s$) = 0 OR LEN(replace$) = 0 THEN
    19.         strReplace$ = s$: EXIT FUNCTION
    20.     ELSE
    21.         LR = LEN(replace$): lNew = LEN(new$)
    22.     END IF
    23.  
    24.     sCopy$ = s$ ' otherwise s$ would get changed
    25.     p = INSTR(sCopy$, replace$)
    26.     WHILE p
    27.         sCopy$ = MID$(sCopy$, 1, p - 1) + new$ + MID$(sCopy$, p + LR)
    28.         p = INSTR(p + lNew, sCopy$, replace$)
    29.     WEND
    30.     strReplace$ = sCopy$
    31.  
    32.  

    You can read the function name as String Replace or Straight Replace, no Swiss Army knife here!
    « Last Edit: July 28, 2020, 12:02:02 pm by bplus »