Logo

Programming-Idioms

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

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;
#include <iostream>
#include <string>
#include <algorithm>
::std::cout << (a
  | ::std::views::transform([](int const i){return ::std::to_string(i);})
  | ::std::views::join_with(::std::string(", "))
  | ::std::ranges::to<::std::string>());

New implementation...
< >
tkoenig