-- Demonstrates array slices
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
procedure slicedemo is
-- A String is an (unconstrained) array of characters
s: String(1..10) := "abcdefghij";
m, n: Integer;
type MyArray is array (1 .. 10) of Integer;
procedure put(a: MyArray) is
begin
for i in a'range loop
put(a(i), width => 4);
end loop;
new_line;
end put;
a: MyArray := (11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
b: MyArray := (81, 82, 83, 84, 85, 86, 87, 88, 89, 90);
begin
put_line( s(2 .. 5) ); -- A string slice
m := 3;
n := 5;
put_line( s(m .. n) ); -- Dynamic slice bounds
a(2 .. 4) := (-1, -2, -3); -- Slice assigned to an aggregate value
-- Warning of runtime error if aggregate is wrong size
put(a);
-- Assign one slice to another
a(1 .. 3) := a(8 .. 10);
put(a);
-- When slices overlap, the values shift
a(1 .. 9) := a(2 .. 10);
put(a);
-- a(1 .. 3) := b; -- Will compile but warns of runtime error
a(1 .. 3) := b(1..3); -- Okay
put(a);
end slicedemo;
-- Output:
--|bcde
--|cde
--| 11 -1 -2 -3 15 16 17 18 19 20
--| 18 19 20 -3 15 16 17 18 19 20
--| 19 20 -3 15 16 17 18 19 20 20
--| 81 82 83 15 16 17 18 19 20 20