2

我正在使用 Dyalog 的“行数组”模式读取文件⎕nget

lines _ _ ← ⎕nget '/usr/share/dict/words' 1

它似乎有效:

          lines[1]
 10th

但是各个元素似乎不是字符数组:

          line ← lines[1]
          line
 10th
          ≢ line
1
          ⍴ line
     

在这里,我们看到第一行的计数为 1,形状为空数组。我无法进一步索引它;lines[1][1]或者line[1]是排名错误。如果我在 RHS 上使用 ⊂,我可以一次将值分配给多个变量,并为每个变量获得相同的行为。但是,如果我在没有左鞋的情况下进行多项作业,我会得到:

          word rest ← line
          word
10th
          ≢ word
4
          ⍴ word
4

最后我们得到了我期望的字符数组!然而,它显然没有与隐藏在其中的任何其他东西分开line;另一个变量是相同的:

          rest
10th
          ≢ rest
4
          ⍴ rest
4
          word ≡ rest
1

值得注意的是,当我查看它时,word它没有前导空格,不像line. 因此,似乎返回的内容矩阵中的各个数组元素⎕nget被进一步包裹在一些没有出现在形状或计数中的东西中,并且不能被索引,但是当我使用解构赋值时,它会解开它们。感觉很像 Common Lisp 中的多值内容。

如果有人可以解释这里发生了什么,我将不胜感激。我觉得我错过了一些非常基本的东西。

4

2 回答 2

4
于 2021-11-12T16:42:17.867 回答
4

使用“行数组”模式读取文件的结果是嵌套数组。它特别是字符向量的嵌套向量,其中每个字符向量都是文本文件中的一行。

例如,在\tmp\test.txt这里:

my text file
has 3
lines

如果我们读到这个,我们可以检查内容

      (content newline encoding) ← ⎕nget'\tmp\test.txt' 1
      ≢ content     ⍝ How many lines?
3
      ≢¨content     ⍝ How long is each line?
12 5 5
      content[2]    ⍝ Indexing returns a scalar (non-simple)
┌─────┐
│has 3│
└─────┘
      2⊃content     ⍝ Use pick to get the contents of the 2nd scalar
has 3
      ⊃content[2]   ⍝ Disclose the non-simple scalar
has 3

正如您可能从在线文档中看到的那样,默认行为⎕NGET是引入一个简单的(非嵌套)字符向量,其中嵌入了换行符。这些通常取决于操作系统。

      (content encoding newline) ← ⎕nget'\tmp\test.txt' 
      newline   ⍝ Unicode code points for line endings in this file  (Microsoft Windows)
13 10
      content
my text file
has 3       
lines       
            
      content ∊ ⎕ucs 10 13
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1

但是使用“行数组”模式,你会得到一个嵌套的结果。

有关嵌套数组和数组模型的快速介绍,请参阅Stefan Kruger 的 LearnAPL 书

于 2021-11-12T16:37:17.337 回答