with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
-- This example uses slices and unconstrained arrays
procedure slicedemo2 is
Type MyArray is array (Positive Range <>) of Integer;
procedure putArray(theArray: MyArray) is
begin
for i in theArray'range loop
put(theArray(i), width=>4);
end loop;
new_line;
end putArray;
-- Create 2 arrays of different size
a: MyArray := (11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
b: MyArray := (81, 82, 83, 84, 85, 86, 87, 88 );
-- Type String is defined package Standard as
-- Type String is array (Positive Range <>) of Character;
-- Equivalent to s: String(1 .. 10) := "abcdefghij";
s: String := "abcdefghij";
m, n: Integer;
begin
-- String slice: We've seen this
put_line( s(2 .. 5) );
putArray(a);
putArray(b);
putArray(b(2 .. 5));
m := 3;
n := 5;
putArray(a(m .. n));
putArray(a(n .. m));
-- Assign a slice with an aggregate value
a(2 .. 4) := (-1, -2, -3);
-- Warning of runtime error if aggregate is wrong size
-- Now let's see what is in a
putArray(a);
-- Assign one slice to another
a(1 .. 3) := a(8 .. 10);
putArray(a);
-- When slices overlap, the values shift
a(1 .. 9) := a(2 .. 10);
putArray(a);
-- Types on both sides of assignment must be identical
-- a(1 .. 3) := b; -- Will compile but warns of runtime error
a(1 .. 8) := b; -- Okay
a(1 .. 3) := b(2 .. 4); -- Okay
putArray(a);
end slicedemo2;