Skip to content

Commit 7c7b8b8

Browse files
committed
stdlib_io: update print_array and its docs
1 parent 036e759 commit 7c7b8b8

File tree

5 files changed

+59
-53
lines changed

5 files changed

+59
-53
lines changed

doc/specs/stdlib_io.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,19 @@ Subroutine
327327

328328
`array`: Shall be a 2D array of `integer`, `real`, or `complex` type. It is an `intent(in)` argument.
329329

330-
`unit`: Shall be an integer containing the output unit. It is an `intent(in)` argument. The default is `6` (standard output).
330+
`unit`: Shall be an integer containing the output unit. It is an `intent(in)` argument. The default is the intrinsic `output_unit` provided by `iso_fortran_env`.
331331

332332
`fmt`: Shall be a character string containing the format for printing the array. It is an `intent(in)` argument. The default is based on [the Formatting constants](#formatting-constants).
333333

334-
`delimiter`: Shall be a character string of length 1 containing the delimiter between array elements. It is an `intent(in)` argument. The default is a `" "` (space).
334+
`delimiter`: Shall be a character string containing the delimiter between array elements. It is an `intent(in)` argument. The default is a `" "` (space).
335335

336-
`brief`: Shall be a logical flag. If `.true.`, the array is printed in a brief format. The default is `.true.`.
336+
`brief`: Shall be a logical flag. The default is `.true.`. If `.true.`, the array is printed in a shortened/abridged version
337+
that shows only the representative portions of large arrays, which is useful for gaining a glimpse of large arrays. Specifically:
338+
+ For arrays with more than 5 rows or columns, it will display:
339+
- First 3 rows and columns;
340+
- Last row and column;
341+
- Eilipsis (`...`) to indicate omitted elements.
342+
+ For arrays with 5 rows or columns or less, it will display the entire array.
337343

338344
### Example
339345

example/io/example_print_array.f90

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ program example_io_print_array
1111
call print_array(array, unit=6, fmt='(i3)', delimiter='|', brief=.true.)
1212

1313
print "(a)", "=== print_array 2 ==="
14-
call print_array(array(:1, :))
14+
call print_array(array(:1, :), delimiter=", ")
1515

1616
end program example_io_print_array

src/stdlib_io.fypp

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ module stdlib_io
112112
${t1}$, intent(in) :: array(:, :)
113113
integer, intent(in), optional :: unit
114114
character(len=*), intent(in), optional :: fmt
115-
character(len=1), intent(in), optional :: delimiter
115+
character(len=*), intent(in), optional :: delimiter
116116
logical, intent(in), optional :: brief
117117
end subroutine print_array_${t1[0]}$${k1}$
118118
#:endfor

src/stdlib_io_print_array.fypp

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ contains
1212
${t1}$, intent(in) :: array(:, :)
1313
integer, intent(in), optional :: unit
1414
character(len=*), intent(in), optional :: fmt
15-
character(len=1), intent(in), optional :: delimiter
15+
character(len=*), intent(in), optional :: delimiter
1616
logical, intent(in), optional :: brief
1717

1818
integer :: i, j, unit_, shape_(2)
1919
character(len=:), allocatable :: fmt_
20-
character(len=1) :: delimiter_
21-
character(len=3) :: delim_str
20+
character(len=:), allocatable :: delimiter_
21+
character(len=:), allocatable :: delim_str
2222
logical :: brief_
2323

2424
shape_ = shape(array)
@@ -48,15 +48,15 @@ contains
4848
write (unit_, fmt='(a)', advance='no') delimiter_//"..."//delimiter_
4949
write (unit_, fmt=fmt_) array(i, shape_(2))
5050
end do
51-
write (unit_, fmt='(a)') ":"
51+
write (unit_, fmt='(a)') "..."
5252
write (unit_, fmt=fmt_, advance='no') array(shape_(1), :3)
5353
write (unit_, fmt='(a)', advance='no') delimiter_//"..."//delimiter_
5454
write (unit_, fmt=fmt_) array(shape_(1), shape_(2))
5555
else
5656
do i = 1, 3
5757
write (unit_, fmt=fmt_) array(i, :)
5858
end do
59-
write (unit_, fmt='(a)') ":"
59+
write (unit_, fmt='(a)') "..."
6060
write (unit_, fmt=fmt_) array(shape_(1), :)
6161

6262
end if

test/io/test_print_array.f90

+43-43
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ subroutine test_print_rdp(error)
4141
line(1) = " 1.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 ... 0.0000000000000000E+000"
4242
line(2) = " 0.0000000000000000E+000 1.0000000000000000E+000 0.0000000000000000E+000 ... 0.0000000000000000E+000"
4343
line(3) = " 0.0000000000000000E+000 0.0000000000000000E+000 1.0000000000000000E+000 ... 0.0000000000000000E+000"
44-
line(4) = ":"
44+
line(4) = "..."
4545
line(5) = " 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 ... 1.0000000000000000E+000"
4646
call print_array(a, fh)
4747

@@ -53,11 +53,11 @@ subroutine test_print_rdp(error)
5353
end do
5454

5555
rewind (fh)
56-
line(1) = "1.00|0.00|0.00|0.00|0.00"
57-
line(2) = "0.00|1.00|0.00|0.00|0.00"
58-
line(3) = "0.00|0.00|1.00|0.00|0.00"
59-
line(4) = "0.00|0.00|0.00|1.00|0.00"
60-
line(5) = "0.00|0.00|0.00|0.00|1.00"
56+
line(1) = "1.00|0.00|0.00|0.00|0.00"
57+
line(2) = "0.00|1.00|0.00|0.00|0.00"
58+
line(3) = "0.00|0.00|1.00|0.00|0.00"
59+
line(4) = "0.00|0.00|0.00|1.00|0.00"
60+
line(5) = "0.00|0.00|0.00|0.00|1.00"
6161
line(6:) = "0.00|0.00|0.00|0.00|0.00"
6262

6363
call print_array(a(:, :5), fh, fmt="(f4.2)", brief=.false., delimiter="|")
@@ -87,7 +87,7 @@ subroutine test_print_rsp(error)
8787
line(1) = " 1.00000000E+00 0.00000000E+00 0.00000000E+00 ... 0.00000000E+00"
8888
line(2) = " 0.00000000E+00 1.00000000E+00 0.00000000E+00 ... 0.00000000E+00"
8989
line(3) = " 0.00000000E+00 0.00000000E+00 1.00000000E+00 ... 0.00000000E+00"
90-
line(4) = ":"
90+
line(4) = "..."
9191
line(5) = " 0.00000000E+00 0.00000000E+00 0.00000000E+00 ... 1.00000000E+00"
9292
call print_array(a, fh)
9393

@@ -99,11 +99,11 @@ subroutine test_print_rsp(error)
9999
end do
100100

101101
rewind (fh)
102-
line(1) = "1.00|0.00|0.00|0.00|0.00"
103-
line(2) = "0.00|1.00|0.00|0.00|0.00"
104-
line(3) = "0.00|0.00|1.00|0.00|0.00"
105-
line(4) = "0.00|0.00|0.00|1.00|0.00"
106-
line(5) = "0.00|0.00|0.00|0.00|1.00"
102+
line(1) = "1.00|0.00|0.00|0.00|0.00"
103+
line(2) = "0.00|1.00|0.00|0.00|0.00"
104+
line(3) = "0.00|0.00|1.00|0.00|0.00"
105+
line(4) = "0.00|0.00|0.00|1.00|0.00"
106+
line(5) = "0.00|0.00|0.00|0.00|1.00"
107107
line(6:) = "0.00|0.00|0.00|0.00|0.00"
108108
call print_array(a(:, :5), fh, fmt="(f4.2)", brief=.false., delimiter="|")
109109

@@ -129,12 +129,12 @@ subroutine test_print_i1(error)
129129
a = eye(10)
130130
open (newunit=fh, status='scratch')
131131

132-
line(1) = "1 0 0 ... 0"
133-
line(2) = "0 1 0 ... 0"
134-
line(3) = "0 0 1 ... 0"
135-
line(4) = ":"
136-
line(5) = "0 0 0 ... 1"
137-
call print_array(a, fh)
132+
line(1) = "1, 0, 0, ..., 0"
133+
line(2) = "0, 1, 0, ..., 0"
134+
line(3) = "0, 0, 1, ..., 0"
135+
line(4) = "..."
136+
line(5) = "0, 0, 0, ..., 1"
137+
call print_array(a, fh, delimiter=", ")
138138

139139
rewind (fh)
140140
do i = 1, 5
@@ -144,11 +144,11 @@ subroutine test_print_i1(error)
144144
end do
145145

146146
rewind (fh)
147-
line(1) = "01;00;00;00;00"
148-
line(2) = "00;01;00;00;00"
149-
line(3) = "00;00;01;00;00"
150-
line(4) = "00;00;00;01;00"
151-
line(5) = "00;00;00;00;01"
147+
line(1) = "01;00;00;00;00"
148+
line(2) = "00;01;00;00;00"
149+
line(3) = "00;00;01;00;00"
150+
line(4) = "00;00;00;01;00"
151+
line(5) = "00;00;00;00;01"
152152
line(6:) = "00;00;00;00;00"
153153
call print_array(a(:, :5), fh, fmt="(i0.2)", brief=.false., delimiter=";")
154154
rewind (fh)
@@ -176,7 +176,7 @@ subroutine test_print_i2(error)
176176
line(1) = "1 0 0 ... 0"
177177
line(2) = "0 1 0 ... 0"
178178
line(3) = "0 0 1 ... 0"
179-
line(4) = ":"
179+
line(4) = "..."
180180
line(5) = "0 0 0 ... 1"
181181

182182
call print_array(a, fh)
@@ -188,13 +188,13 @@ subroutine test_print_i2(error)
188188
end do
189189

190190
rewind (fh)
191-
line(1) = "01;00;00;00;00"
192-
line(2) = "00;01;00;00;00"
193-
line(3) = "00;00;01;00;00"
194-
line(4) = "00;00;00;01;00"
195-
line(5) = "00;00;00;00;01"
196-
line(6:) = "00;00;00;00;00"
197-
call print_array(a(:, :5), fh, fmt="(i0.2)", brief=.false., delimiter=";")
191+
line(1) = "01; 00; 00; 00; 00"
192+
line(2) = "00; 01; 00; 00; 00"
193+
line(3) = "00; 00; 01; 00; 00"
194+
line(4) = "00; 00; 00; 01; 00"
195+
line(5) = "00; 00; 00; 00; 01"
196+
line(6:) = "00; 00; 00; 00; 00"
197+
call print_array(a(:, :5), fh, fmt="(i0.2)", brief=.false., delimiter="; ")
198198
rewind (fh)
199199
do i = 1, 10
200200
call get_line(fh, buffer)
@@ -223,7 +223,7 @@ subroutine test_print_cdp(error)
223223
&0.0000000000000000E+000 0.0000000000000000E+000 ... 0.0000000000000000E+000 0.0000000000000000E+000"
224224
line(3) = " 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 &
225225
&1.0000000000000000E+000 0.0000000000000000E+000 ... 0.0000000000000000E+000 0.0000000000000000E+000"
226-
line(4) = ":"
226+
line(4) = "..."
227227
line(5) = " 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 &
228228
&0.0000000000000000E+000 0.0000000000000000E+000 ... 1.0000000000000000E+000 0.0000000000000000E+000"
229229

@@ -236,11 +236,11 @@ subroutine test_print_cdp(error)
236236
end do
237237

238238
rewind (fh)
239-
line(1) = "1.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
240-
line(2) = "0.00,0.00|1.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
241-
line(3) = "0.00,0.00|0.00,0.00|1.00,0.00|0.00,0.00|0.00,0.00"
242-
line(4) = "0.00,0.00|0.00,0.00|0.00,0.00|1.00,0.00|0.00,0.00"
243-
line(5) = "0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|1.00,0.00"
239+
line(1) = "1.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
240+
line(2) = "0.00,0.00|1.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
241+
line(3) = "0.00,0.00|0.00,0.00|1.00,0.00|0.00,0.00|0.00,0.00"
242+
line(4) = "0.00,0.00|0.00,0.00|0.00,0.00|1.00,0.00|0.00,0.00"
243+
line(5) = "0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|1.00,0.00"
244244
line(6:) = "0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
245245
call print_array(a(:, :5), fh, fmt="(f4.2,"","",f4.2)", brief=.false., delimiter="|")
246246
rewind (fh)
@@ -271,7 +271,7 @@ subroutine test_print_csp(error)
271271
&0.00000000E+00 0.00000000E+00"
272272
line(3) = " 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 1.00000000E+00 0.00000000E+00 ... &
273273
&0.00000000E+00 0.00000000E+00"
274-
line(4) = ":"
274+
line(4) = "..."
275275
line(5) = " 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 ... &
276276
&1.00000000E+00 0.00000000E+00"
277277

@@ -284,11 +284,11 @@ subroutine test_print_csp(error)
284284
end do
285285

286286
rewind (fh)
287-
line(1) = " 1.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
288-
line(2) = " 0.00, 0.00; 1.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
289-
line(3) = " 0.00, 0.00; 0.00, 0.00; 1.00, 0.00; 0.00, 0.00; 0.00, 0.00"
290-
line(4) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 1.00, 0.00; 0.00, 0.00"
291-
line(5) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 1.00, 0.00"
287+
line(1) = " 1.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
288+
line(2) = " 0.00, 0.00; 1.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
289+
line(3) = " 0.00, 0.00; 0.00, 0.00; 1.00, 0.00; 0.00, 0.00; 0.00, 0.00"
290+
line(4) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 1.00, 0.00; 0.00, 0.00"
291+
line(5) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 1.00, 0.00"
292292
line(6:) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
293293
call print_array(a(:, :5), fh, fmt="(1x,f4.2,"","",1x,f4.2)", brief=.false., delimiter=";")
294294
rewind (fh)

0 commit comments

Comments
 (0)