Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #294 Print a comma-separated list of integers

Given an array a containing the three values 1, 12, 42, print out
"1, 12, 42" with a comma and a space after each integer except the last one.

with Ada.Text_IO;
declare
   A : array (1 .. 3) of Integer := (1, 12, 42);
begin
   for I in A'Range loop
      Ada.Text_IO.Put (A (I)'Image);
      if I /= A'Last then
         Ada.Text_IO.Put (", ");
      end if;
   end loop;
end;
print(a.join(", "));
  integer, dimension(:), allocatable :: a
  a = [1,12,42]
  write (*,'(*(I0:", "))') a
import "fmt"
a := []int{1, 12, 42}

for i, j := range a {
	if i > 0 {
		fmt.Print(", ")
	}
	fmt.Print(j)
}
int[] a = new int[] { 1, 12, 42 };
for(int index = 0; index < a.length; index++) {
	if(index != 0) {
		System.out.print(", ");
	}
	System.out.print(a[index]);
}
var
  a: array of integer;
  i: Integer;
begin
  a := [1,12,42];
  for i := Low(a) to High(a) do
  begin
    write(a[i]);
    if i <> High(a) then write(', ');
  end;
end.
@a = qw (1 12 42);
print join(", ",@a),"\n";
a = [1, 12, 42]
print(*a, sep=', ')
puts a.join(", ")
let a = [1, 12, 42];
println!("{}", a.map(|i| i.to_string()).join(", "))

New implementation...
< >
tkoenig