On 16.06.2024 02:02, Rick McGuire wrote:
Um,

a = .stream~new(filename)~arrayin

is a one liner that reads into an array.

Yes, that is the beauty of cascading messages! However, the file needs to be closed (e.g. if one wishes to replace it later) which is not possible in the same line, rather another statement needs to be issued to close the stream, actually for closing the stream one needs a variable to store the stream temporarily, hence two more statements:

    s=.stream~new(filename)
    a=s~arrayin
    s~close

    ... some other code in between ...

    .stream~new(filename)~~open("replace")~~arrayout(a)~close

With the suggested class methods it would become possible to do instead:

   a=.stream~arrayin(filename)

   ... some other code in between ...

   .stream~arrayout(filename,a)

which is much simpler, shorter, hence easier to code, easier to comprehend and as a result less error prone.

---rony



On Sat, Jun 15, 2024 at 7:50 PM Gilbert Barmwater <gi...@bellsouth.net> 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.

    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.

    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.

    Gil

    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

Reply via email to