Logo

Programming-Idioms

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

Idiom #53 Join a list of strings

Concatenate elements of string list x joined by the separator ", " to create a single string y.

Turning the strings "eggs", "butter", "milk" into the string "eggs, butter, milk"
  write (unit=y,fmt='(*(A,:,", "))') x

This uses Fortran's internal write to write to the string y.

The star * in the format descriptor (an extension, not in the Fortran standard) makes sure that the format in the brackets following is repeated as often as possible, and the : means skip anything afterwards (here, the comma) if there are no more I/O items.
with Ada.Containers.Indefinite_Vectors; use Ada.Containers;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
declare
   Last : Cursor := X.Last;
   Y : Unbounded_String;
         
begin
   for C in X.Iterate loop
      Y := Y & Element (C) & (if C = Last then "" else ", ");
   end loop;
end;

New implementation...