Mike Naples
Mike Naples

Reputation: 57

Ada - How do you read an array from a single line of input?

My question is pretty simple, I have input that looks like this...

0   0   0   1   1   1  -1  -1  -1   1

And I need to store these values into an array but I can't figure it out. This is what I have so far...

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
    type arr is array(1..10) of Integer;
    Data : arr;
begin
    for I in 1..arr'Length loop
        Data(I) := Integer'Value(Get_Line);
    end loop;
end Main;

I know this wrong and it's pretty obvious why this isn't working. I'm trying to store multiple values into a single integer, I need a way to iterate over the input or load all the values at once. How would you do this in Ada?

Upvotes: 2

Views: 2124

Answers (3)

debater
debater

Reputation: 466

If you know that you have 10 elements to read, it can be done a little more simply like this:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Hello is
   A: array (1..10) of Integer;
begin
   for V of A loop
      Get(V);
   end loop;
   for I in A'Range loop
      Put(I, 5);
      Put(": ");
      Put(A(I), 5);
      New_Line;
   end loop;
end Hello;

If you don't actually know how many elements to read in advance, please update the question.

Upvotes: 2

G_Zeus
G_Zeus

Reputation: 71

Even though this already was answered, I'd like to add a couple improvements to Jere's answer.

It is more Ada-like to terminate the recursion using End_Of_File rather than an exception. Plus, it makes the program clearer.

Also, using tail-call recursion instead of normal recursion allows the compiler to perform some optimization.

function Get_Ints(input : in File_Type) return Integer_Array is
    function Get_Ints_Rec(accumulator : in Integer_Array) return Integer_Array is
        value : Integer;
    begin
        if End_Of_File(input) then
            return accumulator;
        else
            begin
               Get(input, value);
            exception
               when Data_Error => -- problem when reading
                  if not End_Of_Line(input) then
                     Skip_Line(input);
                  end if;
                  return Get_Ints_Rec(acc);
            end;
            return Get_Ints_Rec(accumulator & (1 => value));
        end if;
    end Get_Ints_Rec;

    acc : constant Integer_Array(1 .. 0) := (others => 0);
begin
    return Get_Ints_Rec(acc);
end Get_Ints;

Upvotes: 1

Jere
Jere

Reputation: 3641

You can use Get_Line to get the whole line as a string and then Ada.Integer_Text_IO to parse the string:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Hello is
    Line : String := Get_Line;
    Value : Integer;
    Last : Positive := 1;
begin

    while Last < Line'Last loop
        Get(Line(Last..Line'Last),Value,Last);
        Put_Line(Value'Image);  -- Save the value to an array here instead
        Last := Last + 1;    -- Needed to move to the next part of the string
    end loop;

end Hello;

After that, you can load the values into an array in the loop or however you like.

Example output:

$gnatmake -o hello *.adb
gcc -c hello.adb
gnatbind -x hello.ali
gnatlink hello.ali -o hello
$hello
 0
 0
 0
 1
 1
 1
-1
-1
-1
 1

EDIT: Adding a recursive option that is more general. This will read a line from STDIN and recursively concatenate the values into an array. It uses the secondary stack to do so in GNAT.

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_Io;

procedure Hello is

    -- Need an array return type
    type Integer_Array is array (Positive range <>) of Integer;

    -- Recursive function
    function Get_Ints return Integer_Array is

        Value : Integer;

    begin
        -- Read from STDIN using Integer_Text_IO;
        Get(Value);

        -- Concatinate recursively
        return Integer_Array'(1 => Value) & Get_Ints;

    exception
        -- I found different exceptions with different versions
        -- of GNAT, so using "others" to cover all versions
        when others => 
            -- Using Ada2012 syntax here.  If not using Ada2012
            -- then just declare the Empty variable somewhere
            -- and then return it here
            return Empty : Integer_Array(1..0);

    end Get_Ints;

    Result : Integer_Array := Get_Ints;

begin
  Put_Line("Hello, world!");
  Put_Line(Integer'Image(Result'Length));
  for E of Result loop
    Put(Integer'Image(E) & " ");
  end loop;
end Hello;

Upvotes: 5

Related Questions