public
Created

Demo for http://stackoverflow.com/q/13851524/827263 "How to assign all elements but one in ada?"

  • Download Gist
foo.adb
Ada
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Foo is
Element_To_Ignore : Integer := 3;
type Array_Of_Integer is array(Positive Range <>) of Integer;
A : Array_Of_Integer := (5,3,2,6);
B : Array_Of_Integer
:= A(A'First .. Element_To_Ignore-1) &
A(Element_To_Ignore+1 .. A'Last);
 
procedure Print_Array(Name: String; A: Array_Of_Integer) is
begin
Put(Name);
Put(": (");
Put(A'First, Width => 0);
Put("..");
Put(A'Last, Width => 0);
Put(") = (");
for I in A'Range loop
Put(A(I), Width => 0);
if (I < A'Last) then
Put(", ");
end if;
end loop;
Put_Line(")");
end Print_Array;
 
begin
for Element_To_Ignore in A'First - 3 .. A'Last + 3 loop
Put("Element_To_Ignore = "); Put(Element_To_Ignore, Width => 0); Put(" : ");
begin
declare
B : Array_Of_Integer
:= A(A'First .. Element_To_Ignore-1) &
A(Element_To_Ignore+1 .. A'Last);
begin
Print_Array("B", B);
end;
exception
when Constraint_Error =>
Put_Line("Constraint_Error");
end;
end loop;
end Foo;
foo.adb.out
1 2 3 4 5 6 7 8 9 10
Element_To_Ignore = -2 : Constraint_Error
Element_To_Ignore = -1 : Constraint_Error
Element_To_Ignore = 0 : B: (1..4) = (5, 3, 2, 6)
Element_To_Ignore = 1 : B: (2..4) = (3, 2, 6)
Element_To_Ignore = 2 : B: (1..3) = (5, 2, 6)
Element_To_Ignore = 3 : B: (1..3) = (5, 3, 6)
Element_To_Ignore = 4 : B: (1..3) = (5, 3, 2)
Element_To_Ignore = 5 : B: (1..4) = (5, 3, 2, 6)
Element_To_Ignore = 6 : Constraint_Error
Element_To_Ignore = 7 : Constraint_Error

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.