Mark Reed
Mark Reed

Reputation: 95395

What's a traditional way to output each element of an array one at a time?

I'm looking for a compact way to print out each element of an array on its own line that would have worked before 1996.

In Dyalog I can do this:

      ⎕←¨1 2 3
1
2
3

But that's a syntax error in e.g. GNU APL, presumably since quad-gets isn't really a function that you can apply each to. If I turn it into one with a dfn, that works fine:

      { ⎕←ω }¨1 2 3

But I'm assuming there was a non-clunky pre-dfn way to do this without having to go write a tradfn, from which I would potentially learn a lot.

EDIT: Sorry, I didn't realize that the answer would be different for different data types. Reshaping into a column vector works well for numeric values, but if the array is of, say, enclosed character vectors, then reshaping results in the output being indented (or boxed if enabled), which is not the case for the for-each version.

Here's a simple example:

      text ← 5/⊂'Line of text'

This is the desired output:

      { ⎕←ω } ¨ text
Line of text
Line of text
Line of text
Line of text
Line of text

Any suggestions for how else to achieve that?

Neither variant of Mix works in both GNU and Dyalog. In Dyalog, up arrow gets me boxed/indented output (but only one level, instead of reshape's two), while in GNU, it outputs only the first element. Right shoe works as desired in GNU, but returns only the first element in Dyalog. I expected the semantics of common features to be a little more consistent between implementations...

Upvotes: 2

Views: 129

Answers (1)

Adám
Adám

Reputation: 7706

The normal way would have been to reshape the array so that its default display form would have one element per line. For a vector like 1 2 3 reshaping into a 3-row 1-column matrix would do:

      ⎕←3 1⍴1 2 3
1
2
3

Many APLs have monadic (Table) mean "reshape into a matrix by ravelling every major cell":

      ⎕←⍪1 2 3
1
2
3

Try it! If you have nested data, e.g. a vector of character vectors ("strings"), you can mix which technically pads them with spaces if they have unequal lengths, but it is the traditional way of printing lists of e.g. names in a vertical fashion:

      ⎕←⍪'Abe' 'Bonnie' 'Carl' 'Donna'  ⍝ ⍪ has indentation indicating nesting
 Abe
 Bonnie
 Carl
 Donna
      ⎕←↑'Abe' 'Bonnie' 'Carl' 'Donna'
Abe   
Bonnie
Carl  
Donna 

Upvotes: 2

Related Questions