Read & write txt files

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

Read & write txt files

Postby larry316 on Sun Dec 18, 2016 3:15 pm

I read the help file but I am not sure how I can create a txt file, write and then read a txt file.
larry316
 
Posts: 16
Joined: Thu Dec 01, 2016 4:28 pm

Re: Read & write txt files

Postby Morten|Dyalog on Mon Dec 19, 2016 8:22 am

Hi Larry,

The easiest way to write text to a file is to use ⎕NPUT:

Code: Select all
(⊂'This is some text',(⎕UCS 13 10),'2nd line')⎕NPUT'c:\temp\sometext.txt'

You can supply the text as a vector of vectors, or as a simple vector with embedded newlines (as above). Note that you need to enclose the text because you COULD specify the encoding to use as a 2nd element of the left argument.

⎕NGET allows us to read the text back, returning a 3-element vector which has a structure that would be suitable for use as the left argument to ⎕NPUT:

Code: Select all
      (text encoding newlines)←⎕NGET 'c:\temp\sometext.txt'
      text
This is some text                   
Here is a 2nd line                   

      ⍴text
37
      encoding
UTF-8-NOBOM
      newlines
13 10

Note that, although newlines tells us that ⎕NGET detected that the newline sequence found in the file was CRLF (13 10), the text has been "normalised" to have lines separated by a single line feed (10). This is done in order to help you write portable code, and to have text which is easier to process.
User avatar
Morten|Dyalog
 
Posts: 394
Joined: Tue Sep 09, 2008 3:52 pm

Re: Read & write txt files

Postby larry316 on Mon Dec 19, 2016 9:11 pm

Thanks for the help. There is a program in linux that I would like to use. Is there an APL command I could use which will use the linux program and the txt file I made with ⎕NPUT?
larry316
 
Posts: 16
Joined: Thu Dec 01, 2016 4:28 pm

Re: Read & write txt files

Postby Morten|Dyalog on Mon Dec 19, 2016 9:27 pm

You can call a Linux program using the system function ⎕SH with a Linux command as the right argument. Any output from the command should be returned as the result:
Code: Select all
      ⎕SH'ls -l /home/brian'
total 152
-rw-r--r--  1 root  root  89140 Dec  6 16:34 default.dlf
drwxr-xr-x  2 brian brian  4096 Nov 30 04:59 Desktop
drwxrwxr-x 16 brian brian  4096 Dec  6 13:41 DFS
...etc...

If your Linux program creates a file, you should be able to use ⎕NGET to retrieve the contents as described earlier.

If you need to provide the file as input to your Linux program, the way to do that will depend on how that program is constructed. Can you provide any further detail?
User avatar
Morten|Dyalog
 
Posts: 394
Joined: Tue Sep 09, 2008 3:52 pm

Re: Read & write txt files

Postby AndyS|Dyalog on Tue Dec 20, 2016 9:53 am

It's worth taking a look at Calling UNIX Commands in the Dyalog for UNIX UI Guide which describes the use of ⎕SH more fully .. in particular you should note how to deal with redirecting stderr, and in coping with commands that return non-zero exit codes.

Note also that ⎕NGET doesn't work with files which claim to have a size of 0 bytes, but in fact "contain" data; this includes files such as pipes, FIFOs and many of the files in /proc. For these you will have to fall back to using ⎕NTIE, ⎕NREAD, ⎕NUNTIE.
User avatar
AndyS|Dyalog
 
Posts: 235
Joined: Tue May 12, 2009 6:06 pm

Re: Read & write txt files

Postby larry316 on Tue Dec 20, 2016 1:19 pm

This is my goal. I would use APL to create a file in the abc language which is
basically a character file and then use a linux program which I downloaded. The program converts it to a midi file I would then use VLC media player or Banshee to
listen to the midi file.
larry316
 
Posts: 16
Joined: Thu Dec 01, 2016 4:28 pm

Re: Read & write txt files

Postby larry316 on Mon Jan 02, 2017 7:09 pm

Morten|Dyalog wrote:You can call a Linux program using the system function ⎕SH with a Linux command as the right argument. Any output from the command should be returned as the result:
Code: Select all
      ⎕SH'ls -l /home/brian'
total 152
-rw-r--r--  1 root  root  89140 Dec  6 16:34 default.dlf
drwxr-xr-x  2 brian brian  4096 Nov 30 04:59 Desktop
drwxrwxr-x 16 brian brian  4096 Dec  6 13:41 DFS
...etc...

If your Linux program creates a file, you should be able to use ⎕NGET to retrieve the contents as described earlier.

If you need to provide the file as input to your Linux program, the way to do that will depend on how that program is constructed. Can you provide any further detail?



In your example, is it possible to create a variable in Dyalog containing /home/brian and then using ⎕SH to use Linux?

Example: ⎕SH'ls -l variable'
larry316
 
Posts: 16
Joined: Thu Dec 01, 2016 4:28 pm

Re: Read & write txt files

Postby Morten|Dyalog on Mon Jan 02, 2017 8:00 pm

larry316 wrote:is it possible to create a variable in Dyalog containing /home/brian and then using ⎕SH to use Linux?

Example: ⎕SH'ls -l variable'


You could write that as:

Code: Select all
⎕SH 'ls -l ''',variable,''''

The above creates an array of characters where the contents of (variable) have been surrounded by single quotes, and that array is passed a the argument to the ⎕SH system function..
User avatar
Morten|Dyalog
 
Posts: 394
Joined: Tue Sep 09, 2008 3:52 pm

Re: Read & write txt files

Postby larry316 on Tue Jan 03, 2017 3:17 pm

Morten|Dyalog wrote:
larry316 wrote:is it possible to create a variable in Dyalog containing /home/brian and then using ⎕SH to use Linux?

Example: ⎕SH'ls -l variable'


You could write that as:

Code: Select all
⎕SH 'ls -l ''',variable,''''

The above creates an array of characters where the contents of (variable) have been surrounded by single quotes, and that array is passed a the argument to the ⎕SH system function..


Thank you very much,that's exactly what I wanted.
larry316
 
Posts: 16
Joined: Thu Dec 01, 2016 4:28 pm

Re: Read & write txt files

Postby larry316 on Sun Jan 08, 2017 5:05 pm

Morten|Dyalog wrote:Hi Larry,

The easiest way to write text to a file is to use ⎕NPUT:

Code: Select all
(⊂'This is some text',(⎕UCS 13 10),'2nd line')⎕NPUT'c:\temp\sometext.txt'

You can supply the text as a vector of vectors, or as a simple vector with embedded newlines (as above). Note that you need to enclose the text because you COULD specify the encoding to use as a 2nd element of the left argument.

⎕NGET allows us to read the text back, returning a 3-element vector which has a structure that would be suitable for use as the left argument to ⎕NPUT:

Code: Select all
      (text encoding newlines)←⎕NGET 'c:\temp\sometext.txt'
      text
This is some text                   
Here is a 2nd line                   

      ⍴text
37
      encoding
UTF-8-NOBOM
      newlines
13 10

Note that, although newlines tells us that ⎕NGET detected that the newline sequence found in the file was CRLF (13 10), the text has been "normalised" to have lines separated by a single line feed (10). This is done in order to help you write portable code, and to have text which is easier to process.



You mentioned create a vector of vectors. I was working on a function that created a numeric for the days of the week and use the numeric to output the date as characters. Example 1 would output Sunday,2 would output Monday etc.
larry316
 
Posts: 16
Joined: Thu Dec 01, 2016 4:28 pm

Next

Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest