OS2 World Community Forum

OS/2, eCS & ArcaOS - Technical => Programming => Topic started by: Per E. Johannessen on April 04, 2019, 06:26:12 pm

Title: Rexx function changestr
Post by: Per E. Johannessen on April 04, 2019, 06:26:12 pm
AFAIK the function "changestr" is not implemented in classic REXX, but seems to be supported by Object REXX.
Does anyone know about a library with this function for classic REXX, or a "workaround" to easily achieve what "changestr" does?

---
CHANGESTR(needle, haystack, newneedle)

This function was added by the ANSI-1996 standard. It replaces all occurrences of string needle in string haystack with string newneedle. Returns the haystack if needle is not found.

Examples --

changestr(‘x’,’abcx’,’d’) == abcd
changestr(‘x’,’abcc’,’d’) == abcc        /* needle was not found in haystack */
---
Title: Re: Rexx function changestr
Post by: Andreas Schnellbacher on April 04, 2019, 07:48:13 pm
ChangeStr is supported by Classic REXX. You just need not-to-old DLLs. (The same applies to the functions in REXXUTIL.DLL.)

Here's my proc for E, the macro language for EPM:

Code: [Select]
; Like ChangeStr in REXX.
defproc ChangeStr( Search, SourceString, Replace)
   OutString = SourceString
   pStart = 1
   do forever
      pSearch = pos( Search, OutString, pStart)
      if pSearch > 0 then
         OutString = delstr( OutString, pSearch, length( Search))
         OutString = insertstr( Replace, OutString, pSearch - 1)
         pStart = pSearch + length( Replace)
      else
         leave
      endif
   enddo
   return OutString

In REXX, this similar one should work:

Code: [Select]
ChangeStr2:
   parse arg Search, SourceString, Replace
   OutString = SourceString
   pStart = 1
   do forever
      pSearch = pos( Search, OutString, pStart)
      if pSearch > 0 then
      do
         OutString = delstr( OutString, pSearch, length( Search))
         OutString = insert( Replace, OutString, pSearch - 1)
         pStart = pSearch + length( Replace)
      end
      else
         leave
   end
   return OutString
Title: Re: Rexx function changestr
Post by: Per E. Johannessen on April 04, 2019, 09:23:26 pm
Thank you Andreas,

In the meantime I've tried the function "translate" and it works as needed.

I'm using the Classic REXX that comes with ArcaOS and took for granted that I had the latest DLL's.
(All the rexx dll's installed are dated 06.09.2000.)

Title: Re: Rexx function changestr
Post by: David Graser on April 04, 2019, 09:52:00 pm
ChangeStr is supported by Classic REXX. You just need not-to-old DLLs. (The same applies to the functions in REXXUTIL.DLL.)


Hobbes has a rexxutil.dll dated 2008.  ArcaOS's dll is dated 2000.

http://hobbes.nmsu.edu/download/pub/os2/dev/rexx/rexxutil.zip

You might want to check here for anything you can use.

http://hobbes.nmsu.edu/h-browse.php?button=Browse&dir=%2Fpub%2Fos2%2Fdev%2Fdll&sort=date


Title: Re: Rexx function changestr
Post by: Andreas Schnellbacher on April 04, 2019, 10:12:00 pm
Hobbes has a rexxutil.dll dated 2008.
That's Mike Green's valuable version of a OSS REXXUTIL clone.

Additionally, CHANGESTR is not defined in REXXUTIL.DLL. BTW: One difference of TRANSLATE and CHANGESTR is that TRANSLATE operates on characters and CHANGESTR on strings of any length.
Title: Re: Rexx function changestr
Post by: Per E. Johannessen on April 04, 2019, 11:16:36 pm
Anyone knows which DLL contains CHANGESTR ?

(By the way, the reason I need this "search/replace" routine is that DBExpert has trouble calculating numbers in forms on systems where locale configuration for decimal symbol is a comma.)
Title: Re: Rexx function changestr
Post by: David Graser on April 04, 2019, 11:54:11 pm
Hobbes has a rexxutil.dll dated 2008.
That's Mike Green's valuable version of a OSS REXXUTIL clone.

Additionally, CHANGESTR is not defined in REXXUTIL.DLL. BTW: One difference of TRANSLATE and CHANGESTR is that TRANSLATE operates on characters and CHANGESTR on strings of any length.

I noticed he did include the sources if anyone some day wants to improve on the dll.

I found this interesting article on the subject.

http://www.scoug.com/os24u/2002/scoug202.changestr.html

Title: Re: Rexx function changestr
Post by: xynixme on April 05, 2019, 11:26:13 am
CHANGESTR is an Object Rexx built-in function.

The OREXX INF help file includes a Rexx version, so you can write and call your own CHANGESTR.CMD function:

Code: [Select]
result=''
$tempx=1;
do forever
  $tempy=pos(needle,haystack,$tempx)
  if $tempy=0 then leave
  result=result||substr(haystack,$tempx,$tempy-$tempx)||newneedle
  $tempx=$tempy+length(needle)
  end
result=result||substr(haystack,$tempx)
Title: Re: Rexx function changestr
Post by: Reksa21 on April 24, 2021, 04:57:47 pm
CHANGESTR() is also supported by KEXX in Kedit for Windows version 1.6.1.

I only learned this recently, been using TRANSLATE() since 1988:)
Title: Re: Rexx function changestr
Post by: Wendy Krieger on October 08, 2021, 12:06:59 pm
Regina has Changestr().