1

I'm learning Ada and I am trying to get a very simple loop working (using the new iterators syntax from Ada 2012). I cannot see what's wrong...

with Ada.Text_IO;
use Ada.Text_IO;

procedure Arrays is
  type V is array (0 .. 1, 0 .. 2) of Natural;
  A : V := ((10, 20, 30), (400, 500, 600));
begin
  for E of A loop  --  compiler error here!
    E := E + 1;
  end loop;
end Arrays;

My compile command is "$ gnatmake -gnaty -gnaty2 -gnat12 arrays" (for pedantic style enforcement and to enable 2012 features). The compiler error is

arrays.adb:8:14: too few subscripts in array reference

(I'm using gnatmake 4.6 on a Raspi).

This code is pieced together from John Barnes' book "Programming in Ada 2012" p.120-121. I've trimmed down this code as much as I can, that's why it doesn't do much. As far as I can see, it's effetively identical to the examples in the book.

What am I doing wrong?

  • 1
    Possibly ... using gcc-4.6. Ada-2012 support has improved a lot since then. gcc-4.9 should be available for the R-Pi and do what you want. (EDIT : gnatmake 4.9 swallows your example silently, making an executable) –  Jul 10 '15 at 13:35
  • Thank you for investigating that for me. My raspi seems to think that 4.6 is still the latest version of the gnat tools. I'll have a hunt around to see if I can manually build 4.9 from sourcecode. –  Jul 10 '15 at 13:53
  • Raspian is built on Debian, (plus Debian itself is available for the Pi) and 4.9 is standard in Debian Jessie, so depending on your distro it should be available. –  Jul 10 '15 at 13:57
  • Can't get 4.9 working on my Pi, might hop over to my proper computer and stick a recent Debian distro on there. Thanks for the help. –  Jul 10 '15 at 14:46
  • it compiles ok with GNAT 2015 GPL. – Nasser Jul 12 '15 at 05:36
  • @BrianDrummond, As a test I have installed Debian Jessie on a virtual and can confirm that it does indeed compile this code without complaint. My next step is to drop this OS onto my netbook so I can do further evaluation of Ada. Thank you very much for this suggestion. :) –  Jul 12 '15 at 13:05

1 Answers1

2

Use a nested array.

    procedure Iterator is
        type rows is array (0 .. 2) of Natural;
        type columns is array (0 .. 1) of rows;
        A : columns := ((10, 20, 30), (400, 500, 600));
    begin
        for column of A loop
            for E of column loop
                E := E + 1;
                put_line (e'img);
            end loop;
        end loop;
    end Iterator;
Wayne
  • 66
  • 4