Discussion:
How to read / write an array from within a function?
(too old to reply)
Massimo Nespolo
2009-02-05 13:29:02 UTC
Permalink
Greetings,
the User's Guide manual explains how to perform and I/O operation with the
FILE command. It says

rdata ← FILE filespec
rc ← wdata FILE filespec

where

filespec
A character vector that identifies a file. It may be a simple file name,
or may include a path
specification.
On Windows filespec may contain Unicode characters.

rdata
The contents of the file as a simple character vector. Any line or record
separators are retained as they
existed in the file.
Any error conditions encountered while reading a file are reported by
returning a numeric result in place
of the data.

wdata
The data to be written to the file.
If wdata is a simple character object, it is ravelled and written to the
file exactly as given. If the file
already exists, the old contents are replaced by the new data. If the
character object is null, a 0-length
file will be written.
If wdata is a non-character null, the file will be erased.

rc
The return code from writing a file; normally zero. Nonzero return codes
are system dependent. The
CHECK_ERROR function can be used to obtain more information.

There are however no examples. I need to read some arrays from my hard
disk, but I am not sure about the format used. So I did a test in the
opposite direction, namely creating an arrays and trying to write it on
disk (directly in the Session Manager), to later read it in another array.
I created a simple array

A ← 12 3rho iota36

Then, I wanted to write it on disk. I tried

RC ← A FILE TEST.TXT

and got error. Without .TXT, with quotation marks in different
combinations - nothing, always error. I must being misunderstanding the
instructions but for sure one example would have helped.

Can somebody indicate me the right direction?

Many thanks in advance.

Massimo Nespolo
Nancy, France.
Nancy Wheeler
2009-02-05 17:56:01 UTC
Permalink
Hi Massimo,

The FILE external function is only one of several options for doing file
I/O from APL2. It is the simplest option - it always reads a file as a
byte stream, and will only write simple character vectors out.
It also is limited in that it always reads and writes the entire file,
you have no option to read or write a subset of the file.

If you want to store arbitrary APL2 arrays in a file, and the ability to
access individual items/records in a file, you can use AP 211 to store
arrays by name or Processor 12 to store them sequentially.
Processor 12 also has options to process text files.

In the APL2 User's Guide, see section "AP 211" under "Supplied Auxiliary
Processors" for information (and examples) of using AP 211, or section
"Processor 12 - Files As Arrays" under "Associated Processors" for
information on Processor 12.

Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Greetings,
the User's Guide manual explains how to perform and I/O operation with
the FILE command. It says
rdata ← FILE filespec
rc ← wdata FILE filespec
where
filespec
A character vector that identifies a file. It may be a simple file
name, or may include a path
specification.
On Windows filespec may contain Unicode characters.
rdata
The contents of the file as a simple character vector. Any line or
record separators are retained as they
existed in the file.
Any error conditions encountered while reading a file are reported by
returning a numeric result in place
of the data.
wdata
The data to be written to the file.
If wdata is a simple character object, it is ravelled and written to
the file exactly as given. If the file
already exists, the old contents are replaced by the new data. If the
character object is null, a 0-length
file will be written.
If wdata is a non-character null, the file will be erased.
rc
The return code from writing a file; normally zero. Nonzero return
codes are system dependent. The
CHECK_ERROR function can be used to obtain more information.
There are however no examples. I need to read some arrays from my hard
disk, but I am not sure about the format used. So I did a test in the
opposite direction, namely creating an arrays and trying to write it on
disk (directly in the Session Manager), to later read it in another
array. I created a simple array
A ← 12 3rho iota36
Then, I wanted to write it on disk. I tried
RC ← A FILE TEST.TXT
and got error. Without .TXT, with quotation marks in different
combinations - nothing, always error. I must being misunderstanding the
instructions but for sure one example would have helped.
Can somebody indicate me the right direction?
Many thanks in advance.
Massimo Nespolo
Nancy, France.
Massimo Nespolo
2009-02-05 18:35:46 UTC
Permalink
Hi Nancy,
how nice for somebody living in Nancy (the French town) to get help from
Nancy (the lady)! ;-)
I apologise if my questions sound dumb, I have worked a bit with APL back
in the early '80s, learnt a bit of hte language on a small computer, but
the interactions with the interpreter have since then changed a lot.
I have read the secton in the User's Guide you have pointed me at and
tried to reproduce the first example. The problem is that I immediately
got an error. I understand I have first of all offer a shared variable to
AP 211 and this is done - in the exammple - by the instruction 211 SVOFFER
'SHR211'. I tried it as such but I got:

VALUE ERROR
211 SVOFFER 'SHR211'
^

It looks like the interpreter does not recognize the function SVOFFER. Do
I have to do / set / install something before APL2 can access this
function?

Thanks so much for your help!

Massimo
Post by Nancy Wheeler
Hi Massimo,
The FILE external function is only one of several options for doing file
I/O from APL2. It is the simplest option - it always reads a file as a
byte stream, and will only write simple character vectors out.
It also is limited in that it always reads and writes the entire file,
you have no option to read or write a subset of the file.
If you want to store arbitrary APL2 arrays in a file, and the ability to
access individual items/records in a file, you can use AP 211 to store
arrays by name or Processor 12 to store them sequentially.
Processor 12 also has options to process text files.
In the APL2 User's Guide, see section "AP 211" under "Supplied Auxiliary
Processors" for information (and examples) of using AP 211, or section
"Processor 12 - Files As Arrays" under "Associated Processors" for
information on Processor 12.
Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Greetings,
the User's Guide manual explains how to perform and I/O operation with
the FILE command. It says
rdata ← FILE filespec
rc ← wdata FILE filespec
where
filespec
A character vector that identifies a file. It may be a simple file
name, or may include a path
specification.
On Windows filespec may contain Unicode characters.
rdata
The contents of the file as a simple character vector. Any line or
record separators are retained as they
existed in the file.
Any error conditions encountered while reading a file are reported by
returning a numeric result in place
of the data.
wdata
The data to be written to the file.
If wdata is a simple character object, it is ravelled and written to
the file exactly as given. If the file
already exists, the old contents are replaced by the new data. If the
character object is null, a 0-length
file will be written.
If wdata is a non-character null, the file will be erased.
rc
The return code from writing a file; normally zero. Nonzero return
codes are system dependent. The
CHECK_ERROR function can be used to obtain more information.
There are however no examples. I need to read some arrays from my
hard disk, but I am not sure about the format used. So I did a test in
the opposite direction, namely creating an arrays and trying to write
it on disk (directly in the Session Manager), to later read it in
another array. I created a simple array
A ← 12 3rho iota36
Then, I wanted to write it on disk. I tried
RC ← A FILE TEST.TXT
and got error. Without .TXT, with quotation marks in different
combinations - nothing, always error. I must being misunderstanding
the instructions but for sure one example would have helped.
Can somebody indicate me the right direction?
Many thanks in advance.
Massimo Nespolo
Nancy, France.
Nancy Wheeler
2009-02-05 23:52:15 UTC
Permalink
Hi Massimo,

The SVO function is found in the public workspace 1 UTILITY.
The command
)COPY 1 UTILITY SVOFFER
will bring it into the active workspace.

Regards,
Nancy Wheeler
APL Prroducts and Services
IBM
Post by Massimo Nespolo
Hi Nancy,
how nice for somebody living in Nancy (the French town) to get help
from Nancy (the lady)! ;-)
I apologise if my questions sound dumb, I have worked a bit with APL
back in the early '80s, learnt a bit of hte language on a small
computer, but the interactions with the interpreter have since then
changed a lot.
I have read the secton in the User's Guide you have pointed me at and
tried to reproduce the first example. The problem is that I immediately
got an error. I understand I have first of all offer a shared variable
to AP 211 and this is done - in the exammple - by the instruction 211
VALUE ERROR
211 SVOFFER 'SHR211'
^
It looks like the interpreter does not recognize the function SVOFFER.
Do I have to do / set / install something before APL2 can access this
function?
Thanks so much for your help!
Massimo
Post by Nancy Wheeler
Hi Massimo,
The FILE external function is only one of several options for doing
file I/O from APL2. It is the simplest option - it always reads a
file as a byte stream, and will only write simple character vectors out.
It also is limited in that it always reads and writes the entire
file, you have no option to read or write a subset of the file.
If you want to store arbitrary APL2 arrays in a file, and the ability
to access individual items/records in a file, you can use AP 211 to
store arrays by name or Processor 12 to store them sequentially.
Processor 12 also has options to process text files.
In the APL2 User's Guide, see section "AP 211" under "Supplied
Auxiliary Processors" for information (and examples) of using AP 211,
or section
"Processor 12 - Files As Arrays" under "Associated Processors" for
information on Processor 12.
Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Greetings,
the User's Guide manual explains how to perform and I/O operation
with the FILE command. It says
rdata ← FILE filespec
rc ← wdata FILE filespec
where
filespec
A character vector that identifies a file. It may be a simple file
name, or may include a path
specification.
On Windows filespec may contain Unicode characters.
rdata
The contents of the file as a simple character vector. Any line or
record separators are retained as they
existed in the file.
Any error conditions encountered while reading a file are reported
by returning a numeric result in place
of the data.
wdata
The data to be written to the file.
If wdata is a simple character object, it is ravelled and written to
the file exactly as given. If the file
already exists, the old contents are replaced by the new data. If
the character object is null, a 0-length
file will be written.
If wdata is a non-character null, the file will be erased.
rc
The return code from writing a file; normally zero. Nonzero return
codes are system dependent. The
CHECK_ERROR function can be used to obtain more information.
There are however no examples. I need to read some arrays from my
hard disk, but I am not sure about the format used. So I did a test
in the opposite direction, namely creating an arrays and trying to
write it on disk (directly in the Session Manager), to later read
it in another array. I created a simple array
A ← 12 3rho iota36
Then, I wanted to write it on disk. I tried
RC ← A FILE TEST.TXT
and got error. Without .TXT, with quotation marks in different
combinations - nothing, always error. I must being misunderstanding
the instructions but for sure one example would have helped.
Can somebody indicate me the right direction?
Many thanks in advance.
Massimo Nespolo
Nancy, France.
Massimo Nespolo
2009-02-15 18:13:50 UTC
Permalink
Hi Nancy,
I am still stuck with the I/O commands, which are really mysterious to me.
I refer to Processor 12 chapter in the User's guide. in particular to the
explanation at page 171.
I tried something very simple. I wanted to create a 3x3 matrix, store it
in a text file so that it can later be read in. I wanted to store my
matrix in a file called "test.txt" in D drive. Here is what I tried

MAT <- 3 3rho iota9
('FCW' 'D:\TEST.TXT' '') 12 SQUARE NA 'MAT'

I get a "0" code saying it did not work. I do not understand why - that
means I do not understand how it works.

Generally speaking, what is the easiest and simplest way to write an array
to a file, and to read it in later? In other languages it is a very
straightfoward task (open, read/write, close), in APL seems much more
complex than writing the code of a function.

Thank you again for you help!

Massimo
Post by Nancy Wheeler
Hi Massimo,
The FILE external function is only one of several options for doing file
I/O from APL2. It is the simplest option - it always reads a file as a
byte stream, and will only write simple character vectors out.
It also is limited in that it always reads and writes the entire file,
you have no option to read or write a subset of the file.
If you want to store arbitrary APL2 arrays in a file, and the ability to
access individual items/records in a file, you can use AP 211 to store
arrays by name or Processor 12 to store them sequentially.
Processor 12 also has options to process text files.
In the APL2 User's Guide, see section "AP 211" under "Supplied Auxiliary
Processors" for information (and examples) of using AP 211, or section
"Processor 12 - Files As Arrays" under "Associated Processors" for
information on Processor 12.
Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Greetings,
the User's Guide manual explains how to perform and I/O operation with
the FILE command. It says
rdata © FILE filespec
rc © wdata FILE filespec
where
filespec
A character vector that identifies a file. It may be a simple file
name, or may include a path
specification.
On Windows filespec may contain Unicode characters.
rdata
The contents of the file as a simple character vector. Any line or
record separators are retained as they
existed in the file.
Any error conditions encountered while reading a file are reported by
returning a numeric result in place
of the data.
wdata
The data to be written to the file.
If wdata is a simple character object, it is ravelled and written to
the file exactly as given. If the file
already exists, the old contents are replaced by the new data. If the
character object is null, a 0-length
file will be written.
If wdata is a non-character null, the file will be erased.
rc
The return code from writing a file; normally zero. Nonzero return
codes are system dependent. The
CHECK_ERROR function can be used to obtain more information.
There are however no examples. I need to read some arrays from my hard
disk, but I am not sure about the format used. So I did a test in the
opposite direction, namely creating an arrays and trying to write it on
disk (directly in the Session Manager), to later read it in another
array. I created a simple array
A © 12 3rho iota36
Then, I wanted to write it on disk. I tried
RC © A FILE TEST.TXT
and got error. Without .TXT, with quotation marks in different
combinations - nothing, always error. I must being misunderstanding the
instructions but for sure one example would have helped.
Can somebody indicate me the right direction?
Many thanks in advance.
Massimo Nespolo
Nancy, France.
Nancy Wheeler
2009-02-16 17:28:13 UTC
Permalink
Massimo,

There are three basic things wrong with what you have done.

1. The QuadNA must be the first thing you do. You first assigned M a
value which creates a variable M in the workspace. Once that is done
the QuadNA to M cannot be accomplished because M already exists locally
inside the workspace and that is a conflict. You must do the QuadNA
first and then assign the value.

2. If a file is created as type 'F' (text) you cannot directly assign
numbers to it. Only characters can be assigned to type 'F' files.
If you want to put numbers into a file, you must either use type 'A'
(APL) and store the numeric data in its native APL numeric form or
transform the numbers to character form (perhaps using the format
primitive, or the QuadTF function, or the ATR external function).

3. When you create a Processor 12 association to a file, the view of the
file is as a vector, and data is always added by catentation, you cannot
assign the entire array. For example, after creation of MAT by your
statement (with type 'F') you would add lines of text to the file like this:
MAT {assign} MAT , 'THIS IS LINE 1' 'THIS IS LINE 2' 'THIS IS LINE 3'
or, if you replaced the 'F' with an 'A' to use APL format in the file,
then you could say
MAT {assign} MAT, {enclose} 3 3 {rho}{iota}9

To answer the general question, if you want to store one arbitrary APL
array in its native APL format in a file, in my opinion the the simplest
method would be to use AP 211, not Processor 12. Processor 12 is more
geared toward creating large files with multiple arrays arranged in
sequential order, or for plain text files.

Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Hi Nancy,
I am still stuck with the I/O commands, which are really mysterious to me.
I refer to Processor 12 chapter in the User's guide. in particular to the
explanation at page 171.
I tried something very simple. I wanted to create a 3x3 matrix, store it
in a text file so that it can later be read in. I wanted to store my
matrix in a file called "test.txt" in D drive. Here is what I tried
MAT <- 3 3rho iota9
('FCW' 'D:\TEST.TXT' '') 12 SQUARE NA 'MAT'
I get a "0" code saying it did not work. I do not understand why - that
means I do not understand how it works.
Generally speaking, what is the easiest and simplest way to write an array
to a file, and to read it in later? In other languages it is a very
straightfoward task (open, read/write, close), in APL seems much more
complex than writing the code of a function.
Thank you again for you help!
Massimo
Post by Nancy Wheeler
Hi Massimo,
The FILE external function is only one of several options for doing file
I/O from APL2. It is the simplest option - it always reads a file as a
byte stream, and will only write simple character vectors out.
It also is limited in that it always reads and writes the entire file,
you have no option to read or write a subset of the file.
If you want to store arbitrary APL2 arrays in a file, and the ability to
access individual items/records in a file, you can use AP 211 to store
arrays by name or Processor 12 to store them sequentially.
Processor 12 also has options to process text files.
In the APL2 User's Guide, see section "AP 211" under "Supplied Auxiliary
Processors" for information (and examples) of using AP 211, or section
"Processor 12 - Files As Arrays" under "Associated Processors" for
information on Processor 12.
Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Greetings,
the User's Guide manual explains how to perform and I/O operation with
the FILE command. It says
rdata �� FILE filespec
rc �� wdata FILE filespec
where
filespec
A character vector that identifies a file. It may be a simple file
name, or may include a path
specification.
On Windows filespec may contain Unicode characters.
rdata
The contents of the file as a simple character vector. Any line or
record separators are retained as they
existed in the file.
Any error conditions encountered while reading a file are reported by
returning a numeric result in place
of the data.
wdata
The data to be written to the file.
If wdata is a simple character object, it is ravelled and written to
the file exactly as given. If the file
already exists, the old contents are replaced by the new data. If the
character object is null, a 0-length
file will be written.
If wdata is a non-character null, the file will be erased.
rc
The return code from writing a file; normally zero. Nonzero return
codes are system dependent. The
CHECK_ERROR function can be used to obtain more information.
There are however no examples. I need to read some arrays from my hard
disk, but I am not sure about the format used. So I did a test in the
opposite direction, namely creating an arrays and trying to write it on
disk (directly in the Session Manager), to later read it in another
array. I created a simple array
A �� 12 3rho iota36
Then, I wanted to write it on disk. I tried
RC �� A FILE TEST.TXT
and got error. Without .TXT, with quotation marks in different
combinations - nothing, always error. I must being misunderstanding the
instructions but for sure one example would have helped.
Can somebody indicate me the right direction?
Many thanks in advance.
Massimo Nespolo
Nancy, France.
Massimo Nespolo
2009-02-19 10:14:55 UTC
Permalink
Hello Nancy,
OK, I've tried the Processor 211, and here is what I've done.
MAT<-3 3{rho}9
MAT
1 2 3
4 5 6
7 8 9

)COPY 1 UTILITY SVOFFER

211 SVOFFER 'SHR211'
2
SHR211<-'CREATE' 'OUTPUT'
SHR211
0
SHR211<-'USE' 'OUTPUT'
SHR211
0 1024
SHR211<-MAT
SHR211
-4

Now, -4 return code means "type error". No further explanation. What am I
doing wrong?

Let's take the problem the other way round. I want to create an input file
that APL2 should read and process. If possible, I would like to write the
result on hard disk, in a format that is readable with an external text
editor.

In other language - I worked mainly in Fortran - I can open a text editor,
type in my array, save it with a fixed name, and my program will then read
it's content, either because the name of the input file is encoded in the
program, or because I can tell the program the file name. The output is
written in a file - say "output.out" - and I can visualize it again with a
text editor.
I would like to do something similar with APL2. The output part is less
urgent, but I absolutely need to have an external file that contains data
I can modify, without putting the data inside the function - I need to
change my input again and again, it is really cumbersome to change the
function everytime.

The must be some easy way of doing this. I feel dumb being stuck in this
I/O problem but I have spent some hours in reading the User Manual and
haven't solved the problem.

Thank you so much for you help!

Massimo
Post by Nancy Wheeler
Massimo,
There are three basic things wrong with what you have done.
1. The QuadNA must be the first thing you do. You first assigned M a
value which creates a variable M in the workspace. Once that is done
the QuadNA to M cannot be accomplished because M already exists locally
inside the workspace and that is a conflict. You must do the QuadNA
first and then assign the value.
2. If a file is created as type 'F' (text) you cannot directly assign
numbers to it. Only characters can be assigned to type 'F' files.
If you want to put numbers into a file, you must either use type 'A'
(APL) and store the numeric data in its native APL numeric form or
transform the numbers to character form (perhaps using the format
primitive, or the QuadTF function, or the ATR external function).
3. When you create a Processor 12 association to a file, the view of the
file is as a vector, and data is always added by catentation, you cannot
assign the entire array. For example, after creation of MAT by your
MAT {assign} MAT , 'THIS IS LINE 1' 'THIS IS LINE 2' 'THIS IS LINE 3'
or, if you replaced the 'F' with an 'A' to use APL format in the file,
then you could say
MAT {assign} MAT, {enclose} 3 3 {rho}{iota}9
To answer the general question, if you want to store one arbitrary APL
array in its native APL format in a file, in my opinion the the simplest
method would be to use AP 211, not Processor 12. Processor 12 is more
geared toward creating large files with multiple arrays arranged in
sequential order, or for plain text files.
Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Hi Nancy,
I am still stuck with the I/O commands, which are really mysterious to me.
I refer to Processor 12 chapter in the User's guide. in particular to
the explanation at page 171.
I tried something very simple. I wanted to create a 3x3 matrix, store
it in a text file so that it can later be read in. I wanted to store my
matrix in a file called "test.txt" in D drive. Here is what I tried
MAT <- 3 3rho iota9
('FCW' 'D:\TEST.TXT' '') 12 SQUARE NA 'MAT'
I get a "0" code saying it did not work. I do not understand why -
that means I do not understand how it works.
Generally speaking, what is the easiest and simplest way to write an
array to a file, and to read it in later? In other languages it is a
very straightfoward task (open, read/write, close), in APL seems much
more complex than writing the code of a function.
Thank you again for you help!
Massimo
Post by Nancy Wheeler
Hi Massimo,
The FILE external function is only one of several options for doing
file I/O from APL2. It is the simplest option - it always reads a
file as a byte stream, and will only write simple character vectors
out.
It also is limited in that it always reads and writes the entire file,
you have no option to read or write a subset of the file.
If you want to store arbitrary APL2 arrays in a file, and the ability
to access individual items/records in a file, you can use AP 211 to
store arrays by name or Processor 12 to store them sequentially.
Processor 12 also has options to process text files.
In the APL2 User's Guide, see section "AP 211" under "Supplied
Auxiliary Processors" for information (and examples) of using AP 211,
or section
"Processor 12 - Files As Arrays" under "Associated Processors" for
information on Processor 12.
Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Greetings,
the User's Guide manual explains how to perform and I/O operation
with the FILE command. It says
rdata �© FILE filespec
rc �© wdata FILE filespec
where
filespec
A character vector that identifies a file. It may be a simple file
name, or may include a path
specification.
On Windows filespec may contain Unicode characters.
rdata
The contents of the file as a simple character vector. Any line or
record separators are retained as they
existed in the file.
Any error conditions encountered while reading a file are reported
by returning a numeric result in place
of the data.
wdata
The data to be written to the file.
If wdata is a simple character object, it is ravelled and written to
the file exactly as given. If the file
already exists, the old contents are replaced by the new data. If
the character object is null, a 0-length
file will be written.
If wdata is a non-character null, the file will be erased.
rc
The return code from writing a file; normally zero. Nonzero return
codes are system dependent. The
CHECK_ERROR function can be used to obtain more information.
There are however no examples. I need to read some arrays from my
hard disk, but I am not sure about the format used. So I did a test
in the opposite direction, namely creating an arrays and trying to
write it on disk (directly in the Session Manager), to later read it
in another array. I created a simple array
A �© 12 3rho iota36
Then, I wanted to write it on disk. I tried
RC �© A FILE TEST.TXT
and got error. Without .TXT, with quotation marks in different
combinations - nothing, always error. I must being misunderstanding
the instructions but for sure one example would have helped.
Can somebody indicate me the right direction?
Many thanks in advance.
Massimo Nespolo
Nancy, France.
Nancy Wheeler
2009-02-19 18:49:02 UTC
Permalink
Massimo,

The line
SHR211<-MAT
is incorrect.
It should be:
SHR211<-'SET' 'MAT' MAT
You must always specify an AP 211 command first, then the parameters
necessary for that command. The 'SET' AP 211 command requires two
parameters, the name to be used to retrieve the data and the data itself.

As to your second question, perhaps I have taken your original question
too literally. You started out asking about file I/O so I told you
about the direct file I/O methods available to APL2 programs. There is
no direct file I/O method in APL2 which will automatically convert
numeric data to its character representation as it writes it to the
file. So if you want human-readable text files to contain numeric APL2
data you must comvert the data to character representation as a separate
step before writing to the file. You can do that with the format
primitive or the QuadTF system function, depending on what you want the
data to look like in the file. Once you have done that, you can write
character files using Processor 12 type F, AP 210 type D, the FILE
external function or the DeltaFM and DeltaFV functions of Processor 10.

However, there is also an APL2 system command, )OUT, which will write
any APL2 object to a special kind text file, and it <will> automatically
convert objects to a character format for you (using system functions
QuadCR and QuadTF). I assumed you already knew about this command, but
maybe not. If you don't need to share the files with a non-APL2
program, and you don't need to change the files in your text editor,
just read them, maybe it might meet your needs.

Being a system command, )OUT cannot be embedded in an APL function, it
can only be typed in the active APL2 session, but there is also a
function version of this, OUT, in the public workspace 2 FILE.
Be aware however, that while this form is human-readable, the transfer
format used by )OUT is defined very strictly (this is documented in the
APL2 Language Reference) and in order to be processed by the APL2
command )IN the file must remain true to the format. Editing these
files directly is not a good idea.

Nancy Wheeler
APL Products and Services
IBM
Post by Massimo Nespolo
Hello Nancy,
OK, I've tried the Processor 211, and here is what I've done.
MAT<-3 3{rho}9
MAT
1 2 3
4 5 6
7 8 9
)COPY 1 UTILITY SVOFFER
211 SVOFFER 'SHR211'
2
SHR211<-'CREATE' 'OUTPUT'
SHR211
0
SHR211<-'USE' 'OUTPUT'
SHR211
0 1024
SHR211<-MAT
SHR211
-4
Now, -4 return code means "type error". No further explanation. What am
I doing wrong?
Let's take the problem the other way round. I want to create an input
file that APL2 should read and process. If possible, I would like to
write the result on hard disk, in a format that is readable with an
external text editor.
In other language - I worked mainly in Fortran - I can open a text
editor, type in my array, save it with a fixed name, and my program
will then read it's content, either because the name of the input file
is encoded in the program, or because I can tell the program the file
name. The output is written in a file - say "output.out" - and I can
visualize it again with a text editor.
I would like to do something similar with APL2. The output part is less
urgent, but I absolutely need to have an external file that contains
data I can modify, without putting the data inside the function - I
need to change my input again and again, it is really cumbersome to
change the function everytime.
The must be some easy way of doing this. I feel dumb being stuck in
this I/O problem but I have spent some hours in reading the User Manual
and haven't solved the problem.
Thank you so much for you help!
Massimo
Post by Nancy Wheeler
Massimo,
There are three basic things wrong with what you have done.
1. The QuadNA must be the first thing you do. You first assigned M a
value which creates a variable M in the workspace. Once that is done
the QuadNA to M cannot be accomplished because M already exists
locally inside the workspace and that is a conflict. You must do the
QuadNA first and then assign the value.
2. If a file is created as type 'F' (text) you cannot directly assign
numbers to it. Only characters can be assigned to type 'F' files.
If you want to put numbers into a file, you must either use type 'A'
(APL) and store the numeric data in its native APL numeric form or
transform the numbers to character form (perhaps using the format
primitive, or the QuadTF function, or the ATR external function).
3. When you create a Processor 12 association to a file, the view of
the file is as a vector, and data is always added by catentation, you
cannot assign the entire array. For example, after creation of MAT
by your statement (with type 'F') you would add lines of text to the
MAT {assign} MAT , 'THIS IS LINE 1' 'THIS IS LINE 2' 'THIS IS LINE 3'
or, if you replaced the 'F' with an 'A' to use APL format in the
file, then you could say
MAT {assign} MAT, {enclose} 3 3 {rho}{iota}9
To answer the general question, if you want to store one arbitrary
APL array in its native APL format in a file, in my opinion the the
simplest method would be to use AP 211, not Processor 12. Processor
12 is more
geared toward creating large files with multiple arrays arranged in
sequential order, or for plain text files.
Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Hi Nancy,
I am still stuck with the I/O commands, which are really mysterious
to me.
I refer to Processor 12 chapter in the User's guide. in particular
to the explanation at page 171.
I tried something very simple. I wanted to create a 3x3 matrix,
store it in a text file so that it can later be read in. I wanted to
store my matrix in a file called "test.txt" in D drive. Here is what
I tried
MAT <- 3 3rho iota9
('FCW' 'D:\TEST.TXT' '') 12 SQUARE NA 'MAT'
I get a "0" code saying it did not work. I do not understand why -
that means I do not understand how it works.
Generally speaking, what is the easiest and simplest way to write
an array to a file, and to read it in later? In other languages it
is a very straightfoward task (open, read/write, close), in APL
seems much more complex than writing the code of a function.
Thank you again for you help!
Massimo
Post by Nancy Wheeler
Hi Massimo,
The FILE external function is only one of several options for doing
file I/O from APL2. It is the simplest option - it always reads a
file as a byte stream, and will only write simple character vectors
out.
It also is limited in that it always reads and writes the entire
file, you have no option to read or write a subset of the file.
If you want to store arbitrary APL2 arrays in a file, and the
ability to access individual items/records in a file, you can use
AP 211 to store arrays by name or Processor 12 to store them
sequentially.
Processor 12 also has options to process text files.
In the APL2 User's Guide, see section "AP 211" under "Supplied
Auxiliary Processors" for information (and examples) of using AP
211, or section
"Processor 12 - Files As Arrays" under "Associated Processors" for
information on Processor 12.
Nancy Wheeler
APL Products and Services
Post by Massimo Nespolo
Greetings,
the User's Guide manual explains how to perform and I/O operation
with the FILE command. It says
rdata �© FILE filespec
rc �© wdata FILE filespec
where
filespec
A character vector that identifies a file. It may be a simple file
name, or may include a path
specification.
On Windows filespec may contain Unicode characters.
rdata
The contents of the file as a simple character vector. Any line or
record separators are retained as they
existed in the file.
Any error conditions encountered while reading a file are reported
by returning a numeric result in place
of the data.
wdata
The data to be written to the file.
If wdata is a simple character object, it is ravelled and written
to the file exactly as given. If the file
already exists, the old contents are replaced by the new data. If
the character object is null, a 0-length
file will be written.
If wdata is a non-character null, the file will be erased.
rc
The return code from writing a file; normally zero. Nonzero return
codes are system dependent. The
CHECK_ERROR function can be used to obtain more information.
There are however no examples. I need to read some arrays from my
hard disk, but I am not sure about the format used. So I did a
test in the opposite direction, namely creating an arrays and
trying to write it on disk (directly in the Session Manager), to
later read it in another array. I created a simple array
A �© 12 3rho iota36
Then, I wanted to write it on disk. I tried
RC �© A FILE TEST.TXT
and got error. Without .TXT, with quotation marks in different
combinations - nothing, always error. I must being
misunderstanding the instructions but for sure one example would
have helped.
Can somebody indicate me the right direction?
Many thanks in advance.
Massimo Nespolo
Nancy, France.
Loading...