fork download
  1. With Ada.Text_IO; Use Ada.Text_IO;
  2. With Ada.Strings.Fixed;
  3. with Ada.Containers.Indefinite_Vectors;
  4.  
  5. procedure Test is
  6. package Vectors is new Ada.Containers.Indefinite_Vectors(Positive, String);
  7. subtype String_List is Vectors.Vector;
  8.  
  9. function Split(Line : String) return String_List is
  10. Result : String_List;
  11. First : Positive := Line'First;
  12. Last : Natural;
  13. begin
  14. loop
  15. Last := Ada.Strings.Fixed.Index(Line(First..Line'Last), ",");
  16. exit when Last = 0;
  17. Result.Append(Line(First .. Last - 1)); -- Append the item
  18. First := Last + 1; -- set to index after comma
  19. end loop;
  20. Result.Append(Line(First .. Line'Last)); -- Append the last item
  21. return Result;
  22. end Split;
  23.  
  24. Items : constant String_List := Split(Get_Line);
  25. begin
  26. for Item of Items loop
  27. Put_Line(Item);
  28. end loop;
  29.  
  30. Put_Line("2nd item is " & Items(2));
  31. end Test;
Success #stdin #stdout 0.01s 5284KB
stdin
a,b,c,d,e,f,g
stdout
a
b
c
d
e
f
g
2nd item is b