On 16.06.2024 01:49, Gilbert Barmwater wrote:
First let me offer the following way to write the contents of an array to a stream that only
requires a single line:
.stream~new(streamFileName)~~arrayOut(arr [,Lines|Chars])~close
Unfortunately, there is no corresponding "one-liner" for reading the contents of a stream into an
array. This lack of "symmetry" has always bothered me and I have experimented over the years with
possible solutions. Not too long ago I was able to devise a way to finally accomplish it.
Before I reveal it, let me note that the above comments are equally applicable to charOut/charIn
and lineOut/lineIn should one wish to deal with character(s) or a single line of output/input.
My solution is to add additional instance methods to the .stream class named arrayInto, charInto
and lineInto. The difference between them and the similar *In methods is that the first argument
is a variable reference followed by the "standard" *In arguments. These methods perform the
operation of the related *In method and then assign the data - array, character(s) or line - to
the value of the variable reference. So the corresponding "one-liner" for reading the contents of
a stream into an array is:
.stream~new(streamFileName)~~arrayInto(>arr, [Lines|Chars] )~close
But, you might say, ooRexx doesn't allow you to "add" methods to the pre-defined classes like
.stream. That is not strictly true and there are several ways around that. I had planned to talk
about that as part of a planned presentation at the Vienna Symposium next May but, since the
subject has come up now, it looks like I will need to reveal it ahead of time.
That may be helpful for other situations as well! How about revealing it here and talking about it
on wider audience and forum at the symposium? The symposium slides (and articles) are being made
public via the RexxLA symposium page and thereby making your work (world-)widely available.
On a more general, philosophical note, I prefer to find solutions that do NOT require adding
classes, methods, or functions to the base ooRexx but that can be included with a ::requires
statement, similar to the way that, e.g. RxMath functions are handled. I guess that goes back to
the original Rexx idea of keeping the language small but is also consistent with how other
languages provide "libraries" of added functionality.
In principle I agree of "keeping the language small and easy".
Over the course of time it has turned out that reading the content from a file into an array and
writing a file from an array has become more and more helpful.
Hence the motivation of allowing for using a single statement to read into an array from and to
write from an array to a file. It is about folding four statements into a single one.
If anyone is interested in the implementation of the arryInto method, I will post it here and, if
not, will discuss it during the Symposium.
Just to be sure: *yes*, please! :)
---rony
On 6/15/2024 2:38 PM, Rony G. Flatscher wrote:
The Stream class has the handy instance methods arrayIn and arrayOut.
To read the content of a stream into an array the following statements are
necessary:
s=.stream~new(streamFileName)
s~open( [read|both] )
arr=s~arrayIn( [Lines|Chars] )
s~close
To write the content of an array into a stream the following statements are
necessary:
s=.stream~new(streamFileName)
s~open( [write | both [append|replace]] )
s~arrayOut(arr [,Lines|Chars])
s~close
---
It would be very handy, if the Stream class also offered as class methods arrayIn and arrayOut
that would allow one single statement to read from any stream (file) into an array or to write an
array to any stream (file).
class method arrayIn:
arr=.stream~arrayIn( streamFileName [,Lines|Chars] ) -- default: Lines
and
class method arrayOut:
.stream~arrayOut(streamFileName , arr [,append|replace] [,Lines|Chars] )
-- default: append, Lines
What do you think?
Would you see any problems and if so which ones?
---rony
_______________________________________________
Oorexx-devel mailing list
Oorexx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/oorexx-devel