Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ada

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?

like image 549
Mike Naples Avatar asked Dec 17 '25 08:12

Mike Naples


2 Answers

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;
like image 79
Jere Avatar answered Dec 21 '25 06:12

Jere


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.

like image 40
debater Avatar answered Dec 21 '25 08:12

debater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!