Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
stdlib_io: update print_array and its docs
  • Loading branch information
zoziha committed Apr 25, 2025
commit 7c7b8b88adcedd97ecc9dc4bf0534cbd7019a97a
12 changes: 9 additions & 3 deletions doc/specs/stdlib_io.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,19 @@ Subroutine

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

`unit`: Shall be an integer containing the output unit. It is an `intent(in)` argument. The default is `6` (standard output).
`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`.

`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).

`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).
`delimiter`: Shall be a character string containing the delimiter between array elements. It is an `intent(in)` argument. The default is a `" "` (space).

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

### Example

Expand Down
2 changes: 1 addition & 1 deletion example/io/example_print_array.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ program example_io_print_array
call print_array(array, unit=6, fmt='(i3)', delimiter='|', brief=.true.)

print "(a)", "=== print_array 2 ==="
call print_array(array(:1, :))
call print_array(array(:1, :), delimiter=", ")

end program example_io_print_array
2 changes: 1 addition & 1 deletion src/stdlib_io.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module stdlib_io
${t1}$, intent(in) :: array(:, :)
integer, intent(in), optional :: unit
character(len=*), intent(in), optional :: fmt
character(len=1), intent(in), optional :: delimiter
character(len=*), intent(in), optional :: delimiter
logical, intent(in), optional :: brief
end subroutine print_array_${t1[0]}$${k1}$
#:endfor
Expand Down
10 changes: 5 additions & 5 deletions src/stdlib_io_print_array.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ contains
${t1}$, intent(in) :: array(:, :)
integer, intent(in), optional :: unit
character(len=*), intent(in), optional :: fmt
character(len=1), intent(in), optional :: delimiter
character(len=*), intent(in), optional :: delimiter
logical, intent(in), optional :: brief

integer :: i, j, unit_, shape_(2)
character(len=:), allocatable :: fmt_
character(len=1) :: delimiter_
character(len=3) :: delim_str
character(len=:), allocatable :: delimiter_
character(len=:), allocatable :: delim_str
logical :: brief_

shape_ = shape(array)
Expand Down Expand Up @@ -48,15 +48,15 @@ contains
write (unit_, fmt='(a)', advance='no') delimiter_//"..."//delimiter_
write (unit_, fmt=fmt_) array(i, shape_(2))
end do
write (unit_, fmt='(a)') ":"
write (unit_, fmt='(a)') "..."
write (unit_, fmt=fmt_, advance='no') array(shape_(1), :3)
write (unit_, fmt='(a)', advance='no') delimiter_//"..."//delimiter_
write (unit_, fmt=fmt_) array(shape_(1), shape_(2))
else
do i = 1, 3
write (unit_, fmt=fmt_) array(i, :)
end do
write (unit_, fmt='(a)') ":"
write (unit_, fmt='(a)') "..."
write (unit_, fmt=fmt_) array(shape_(1), :)

end if
Expand Down
86 changes: 43 additions & 43 deletions test/io/test_print_array.f90
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ subroutine test_print_rdp(error)
line(1) = " 1.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 ... 0.0000000000000000E+000"
line(2) = " 0.0000000000000000E+000 1.0000000000000000E+000 0.0000000000000000E+000 ... 0.0000000000000000E+000"
line(3) = " 0.0000000000000000E+000 0.0000000000000000E+000 1.0000000000000000E+000 ... 0.0000000000000000E+000"
line(4) = ":"
line(4) = "..."
line(5) = " 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 ... 1.0000000000000000E+000"
call print_array(a, fh)

Expand All @@ -53,11 +53,11 @@ subroutine test_print_rdp(error)
end do

rewind (fh)
line(1) = "1.00|0.00|0.00|0.00|0.00"
line(2) = "0.00|1.00|0.00|0.00|0.00"
line(3) = "0.00|0.00|1.00|0.00|0.00"
line(4) = "0.00|0.00|0.00|1.00|0.00"
line(5) = "0.00|0.00|0.00|0.00|1.00"
line(1) = "1.00|0.00|0.00|0.00|0.00"
line(2) = "0.00|1.00|0.00|0.00|0.00"
line(3) = "0.00|0.00|1.00|0.00|0.00"
line(4) = "0.00|0.00|0.00|1.00|0.00"
line(5) = "0.00|0.00|0.00|0.00|1.00"
line(6:) = "0.00|0.00|0.00|0.00|0.00"

call print_array(a(:, :5), fh, fmt="(f4.2)", brief=.false., delimiter="|")
Expand Down Expand Up @@ -87,7 +87,7 @@ subroutine test_print_rsp(error)
line(1) = " 1.00000000E+00 0.00000000E+00 0.00000000E+00 ... 0.00000000E+00"
line(2) = " 0.00000000E+00 1.00000000E+00 0.00000000E+00 ... 0.00000000E+00"
line(3) = " 0.00000000E+00 0.00000000E+00 1.00000000E+00 ... 0.00000000E+00"
line(4) = ":"
line(4) = "..."
line(5) = " 0.00000000E+00 0.00000000E+00 0.00000000E+00 ... 1.00000000E+00"
call print_array(a, fh)

Expand All @@ -99,11 +99,11 @@ subroutine test_print_rsp(error)
end do

rewind (fh)
line(1) = "1.00|0.00|0.00|0.00|0.00"
line(2) = "0.00|1.00|0.00|0.00|0.00"
line(3) = "0.00|0.00|1.00|0.00|0.00"
line(4) = "0.00|0.00|0.00|1.00|0.00"
line(5) = "0.00|0.00|0.00|0.00|1.00"
line(1) = "1.00|0.00|0.00|0.00|0.00"
line(2) = "0.00|1.00|0.00|0.00|0.00"
line(3) = "0.00|0.00|1.00|0.00|0.00"
line(4) = "0.00|0.00|0.00|1.00|0.00"
line(5) = "0.00|0.00|0.00|0.00|1.00"
line(6:) = "0.00|0.00|0.00|0.00|0.00"
call print_array(a(:, :5), fh, fmt="(f4.2)", brief=.false., delimiter="|")

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

line(1) = "1 0 0 ... 0"
line(2) = "0 1 0 ... 0"
line(3) = "0 0 1 ... 0"
line(4) = ":"
line(5) = "0 0 0 ... 1"
call print_array(a, fh)
line(1) = "1, 0, 0, ..., 0"
line(2) = "0, 1, 0, ..., 0"
line(3) = "0, 0, 1, ..., 0"
line(4) = "..."
line(5) = "0, 0, 0, ..., 1"
call print_array(a, fh, delimiter=", ")

rewind (fh)
do i = 1, 5
Expand All @@ -144,11 +144,11 @@ subroutine test_print_i1(error)
end do

rewind (fh)
line(1) = "01;00;00;00;00"
line(2) = "00;01;00;00;00"
line(3) = "00;00;01;00;00"
line(4) = "00;00;00;01;00"
line(5) = "00;00;00;00;01"
line(1) = "01;00;00;00;00"
line(2) = "00;01;00;00;00"
line(3) = "00;00;01;00;00"
line(4) = "00;00;00;01;00"
line(5) = "00;00;00;00;01"
line(6:) = "00;00;00;00;00"
call print_array(a(:, :5), fh, fmt="(i0.2)", brief=.false., delimiter=";")
rewind (fh)
Expand Down Expand Up @@ -176,7 +176,7 @@ subroutine test_print_i2(error)
line(1) = "1 0 0 ... 0"
line(2) = "0 1 0 ... 0"
line(3) = "0 0 1 ... 0"
line(4) = ":"
line(4) = "..."
line(5) = "0 0 0 ... 1"

call print_array(a, fh)
Expand All @@ -188,13 +188,13 @@ subroutine test_print_i2(error)
end do

rewind (fh)
line(1) = "01;00;00;00;00"
line(2) = "00;01;00;00;00"
line(3) = "00;00;01;00;00"
line(4) = "00;00;00;01;00"
line(5) = "00;00;00;00;01"
line(6:) = "00;00;00;00;00"
call print_array(a(:, :5), fh, fmt="(i0.2)", brief=.false., delimiter=";")
line(1) = "01; 00; 00; 00; 00"
line(2) = "00; 01; 00; 00; 00"
line(3) = "00; 00; 01; 00; 00"
line(4) = "00; 00; 00; 01; 00"
line(5) = "00; 00; 00; 00; 01"
line(6:) = "00; 00; 00; 00; 00"
call print_array(a(:, :5), fh, fmt="(i0.2)", brief=.false., delimiter="; ")
rewind (fh)
do i = 1, 10
call get_line(fh, buffer)
Expand Down Expand Up @@ -223,7 +223,7 @@ subroutine test_print_cdp(error)
&0.0000000000000000E+000 0.0000000000000000E+000 ... 0.0000000000000000E+000 0.0000000000000000E+000"
line(3) = " 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 &
&1.0000000000000000E+000 0.0000000000000000E+000 ... 0.0000000000000000E+000 0.0000000000000000E+000"
line(4) = ":"
line(4) = "..."
line(5) = " 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 &
&0.0000000000000000E+000 0.0000000000000000E+000 ... 1.0000000000000000E+000 0.0000000000000000E+000"

Expand All @@ -236,11 +236,11 @@ subroutine test_print_cdp(error)
end do

rewind (fh)
line(1) = "1.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
line(2) = "0.00,0.00|1.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
line(3) = "0.00,0.00|0.00,0.00|1.00,0.00|0.00,0.00|0.00,0.00"
line(4) = "0.00,0.00|0.00,0.00|0.00,0.00|1.00,0.00|0.00,0.00"
line(5) = "0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|1.00,0.00"
line(1) = "1.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
line(2) = "0.00,0.00|1.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
line(3) = "0.00,0.00|0.00,0.00|1.00,0.00|0.00,0.00|0.00,0.00"
line(4) = "0.00,0.00|0.00,0.00|0.00,0.00|1.00,0.00|0.00,0.00"
line(5) = "0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|1.00,0.00"
line(6:) = "0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00|0.00,0.00"
call print_array(a(:, :5), fh, fmt="(f4.2,"","",f4.2)", brief=.false., delimiter="|")
rewind (fh)
Expand Down Expand Up @@ -271,7 +271,7 @@ subroutine test_print_csp(error)
&0.00000000E+00 0.00000000E+00"
line(3) = " 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 1.00000000E+00 0.00000000E+00 ... &
&0.00000000E+00 0.00000000E+00"
line(4) = ":"
line(4) = "..."
line(5) = " 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 ... &
&1.00000000E+00 0.00000000E+00"

Expand All @@ -284,11 +284,11 @@ subroutine test_print_csp(error)
end do

rewind (fh)
line(1) = " 1.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
line(2) = " 0.00, 0.00; 1.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
line(3) = " 0.00, 0.00; 0.00, 0.00; 1.00, 0.00; 0.00, 0.00; 0.00, 0.00"
line(4) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 1.00, 0.00; 0.00, 0.00"
line(5) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 1.00, 0.00"
line(1) = " 1.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
line(2) = " 0.00, 0.00; 1.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
line(3) = " 0.00, 0.00; 0.00, 0.00; 1.00, 0.00; 0.00, 0.00; 0.00, 0.00"
line(4) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 1.00, 0.00; 0.00, 0.00"
line(5) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 1.00, 0.00"
line(6:) = " 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00; 0.00, 0.00"
call print_array(a(:, :5), fh, fmt="(1x,f4.2,"","",1x,f4.2)", brief=.false., delimiter=";")
rewind (fh)
Expand Down
Loading