comp.lang.ada
 help / color / mirror / Atom feed
* Question about Ada and constant Arrays
@ 2003-11-10 16:49 Stephane Richard
  2003-11-10 17:07 ` Preben Randhol
  2003-11-11  9:34 ` Lutz Donnerhacke
  0 siblings, 2 replies; 7+ messages in thread
From: Stephane Richard @ 2003-11-10 16:49 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 913 bytes --]

Hi everyone,  As the subject line says, the object of the question would be.

I'd like to make an constant array of a given record type and initialize it
upon it's creation.

Type TestRecord Is
Record
        Enabled: Boolean;
        Name:    String(60);
        Value:     Integer;
End Record;

Constant TestArray(1..5): TestRecord = ( (True, "Test1", 1),:
                                                                (False,
"Test2", 2),:
                                                                (False,
"Test3", 4),:
                                                                (False,
"Test4", 8),:
                                                                (True,
"Test5", 16) );

Or something similar to that.  Is that possible? and How?

Thank you all :-).

-- 
"To err is human.  To really screw up, you need C++!"

St�phane Richard
"Ada World" Webmaster
http://www.adaworld.com






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Ada and constant Arrays
  2003-11-10 16:49 Question about Ada and constant Arrays Stephane Richard
@ 2003-11-10 17:07 ` Preben Randhol
  2003-11-10 17:10   ` Stephane Richard
                     ` (2 more replies)
  2003-11-11  9:34 ` Lutz Donnerhacke
  1 sibling, 3 replies; 7+ messages in thread
From: Preben Randhol @ 2003-11-10 17:07 UTC (permalink / raw)


On 2003-11-10, Stephane Richard <stephane.richard@verizon.net> wrote:
> I'd like to make an constant array of a given record type and initialize it
> upon it's creation.

[Delete unknown language (VHDL?)]

Yes it can be done:

---8<-------8<------8<-------

with Ada.Text_IO;  use Ada.Text_IO;

procedure arrayinit
is

   type Test_Record is
      record
        Enabled : Boolean;
        Name    : String(1 .. 5);
        Value   : Integer;
      end record;

   type Test_Array is array (1 .. 5) of Test_Record;

   My_Array : constant Test_Array :=
      (1 => (True, "Test1", 1),
       2 => (False, "Test2", 2),
       3 => (False, "Test3", 4),
       4 => (False, "Test4", 8),
       5 => (True, "Test5", 16));
begin

   --  Printing the values to show that it is correct.

   for i in My_Array'Range loop
      Put_Line (Integer'Image (i) & " => " &
                Boolean'Image (My_Array (i).Enabled) & "," &
                My_Array (i).Name & "," &
                Integer'Image (My_Array (i).Value));
   end loop;

end arrayinit;
---8<-------8<------8<-------


--
"Saving keystrokes is the job of the text editor, not the programming
 language."



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Ada and constant Arrays
  2003-11-10 17:07 ` Preben Randhol
@ 2003-11-10 17:10   ` Stephane Richard
  2003-11-10 17:13     ` Preben Randhol
  2003-11-10 17:11   ` Preben Randhol
  2003-11-11  5:41   ` Nick Roberts
  2 siblings, 1 reply; 7+ messages in thread
From: Stephane Richard @ 2003-11-10 17:10 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1643 bytes --]

excellent, I sincerely doubted it couldn't, I just never actually saw such
an example :-)

Thanks Preben :-)

-- 
"To err is human.  To really screw up, you need C++!"

St�phane Richard
"Ada World" Webmaster
http://www.adaworld.com


"Preben Randhol" <randhol+valid_for_reply_from_news@pvv.org> wrote in
message
news:slrnbqvhev.97p.randhol+valid_for_reply_from_news@kiuk0156.chembio.ntnu.no...
> On 2003-11-10, Stephane Richard <stephane.richard@verizon.net> wrote:
> > I'd like to make an constant array of a given record type and initialize
it
> > upon it's creation.
>
> [Delete unknown language (VHDL?)]
>
> Yes it can be done:
>
> ---8<-------8<------8<-------
>
> with Ada.Text_IO;  use Ada.Text_IO;
>
> procedure arrayinit
> is
>
>    type Test_Record is
>       record
>         Enabled : Boolean;
>         Name    : String(1 .. 5);
>         Value   : Integer;
>       end record;
>
>    type Test_Array is array (1 .. 5) of Test_Record;
>
>    My_Array : constant Test_Array :=
>       (1 => (True, "Test1", 1),
>        2 => (False, "Test2", 2),
>        3 => (False, "Test3", 4),
>        4 => (False, "Test4", 8),
>        5 => (True, "Test5", 16));
> begin
>
>    --  Printing the values to show that it is correct.
>
>    for i in My_Array'Range loop
>       Put_Line (Integer'Image (i) & " => " &
>                 Boolean'Image (My_Array (i).Enabled) & "," &
>                 My_Array (i).Name & "," &
>                 Integer'Image (My_Array (i).Value));
>    end loop;
>
> end arrayinit;
> ---8<-------8<------8<-------
>
>
> --
> "Saving keystrokes is the job of the text editor, not the programming
>  language."





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Ada and constant Arrays
  2003-11-10 17:07 ` Preben Randhol
  2003-11-10 17:10   ` Stephane Richard
@ 2003-11-10 17:11   ` Preben Randhol
  2003-11-11  5:41   ` Nick Roberts
  2 siblings, 0 replies; 7+ messages in thread
From: Preben Randhol @ 2003-11-10 17:11 UTC (permalink / raw)


On 2003-11-10, Preben Randhol <randhol+valid_for_reply_from_news@pvv.org> wrote:
>    My_Array : constant Test_Array :=
>       (1 => (True, "Test1", 1),
>        2 => (False, "Test2", 2),
>        3 => (False, "Test3", 4),
>        4 => (False, "Test4", 8),
>        5 => (True, "Test5", 16));

This can also be written as:

   My_Array : constant Test_Array := ( (True, "Test1", 1), (False,
      "Test2", 2), (False, "Test3", 4), (False, "Test4", 8), (True,
      "Test5", 16) ); 

Depending on your taste :-)

HTH

Preben
-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Ada and constant Arrays
  2003-11-10 17:10   ` Stephane Richard
@ 2003-11-10 17:13     ` Preben Randhol
  0 siblings, 0 replies; 7+ messages in thread
From: Preben Randhol @ 2003-11-10 17:13 UTC (permalink / raw)


On 2003-11-10, Stephane Richard <stephane.richard@verizon.net> wrote:
> excellent, I sincerely doubted it couldn't, I just never actually saw such
> an example :-)

However, note my change in definition of the String in the record. I
guess you understand why.

Preben
-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Ada and constant Arrays
  2003-11-10 17:07 ` Preben Randhol
  2003-11-10 17:10   ` Stephane Richard
  2003-11-10 17:11   ` Preben Randhol
@ 2003-11-11  5:41   ` Nick Roberts
  2 siblings, 0 replies; 7+ messages in thread
From: Nick Roberts @ 2003-11-11  5:41 UTC (permalink / raw)


Just for fun, a possible refinement might be something like this:

~~~

with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure Array_Init_2 is

    package Boolean_IO is new Enumeration_IO(Boolean);
    use Boolean_IO;

    function "+" (Source: in String) return Unbounded_String
       renames To_Unbounded_String;

    procedure Put (Item: in Unbounded_String) is
    begin
       Put( To_String(Item) );
    end;

    type Test_Record is
       record
         Enabled : Boolean;
         Name    : Unbounded_String;
         Value   : Integer;
       end record;

    type Test_Array is array (Positive range <>) of Test_Record;

    My_Array : constant Test_Array :=
       (1 => (True,  +"Test1",            1),
        2 => (False, +"Test2",            2),
        3 => (False, +"Test3",            4),
        4 => (False, +"Test4",            8),
        5 => (True,  +"Test5 (whoopee)", 16),
        6 => (False, +"Test *** 6 ***",  17));
begin

    --  Printing the values to show that it is correct.

    for i in My_Array'Range loop
       Put(i);
       Put(" => ");
       Put(My_Array(i).Enabled);
       Put(", ");
       Put(My_Array(i).Name);
       Put(", ");
       Put(My_Array(i).Value);
       New_Line;
    end loop;

end Array_Init_2;

~~~

I haven't tested this code.

-- 
Nick Roberts




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Question about Ada and constant Arrays
  2003-11-10 16:49 Question about Ada and constant Arrays Stephane Richard
  2003-11-10 17:07 ` Preben Randhol
@ 2003-11-11  9:34 ` Lutz Donnerhacke
  1 sibling, 0 replies; 7+ messages in thread
From: Lutz Donnerhacke @ 2003-11-11  9:34 UTC (permalink / raw)


* Stephane Richard wrote:
> I'd like to make an constant array of a given record type and initialize it
> upon it's creation.

package Hex_Strings is
   type Hex_String is new String;
   function To_Data(h : Hex_String) return String;
   function To_Hex(d : String) return Hex_String;

   type Debug_Types is (Hexify, Dehexify);
   debug : array(Debug_Types) of Boolean := (others => False);
end Hex_Strings;

with Hex_Strings;

package Tests is
   type Constant_String is access constant String;
   type Constant_String_Array is array(Positive range <>) of Constant_String;
   type Constant_Hex_String is access constant Hex_Strings.Hex_String;
   type Constant_Hex_String_Array is array(Positive range <>) of Constant_Hex_String;
end Tests;

with Tests, Hex_Strings;
use Tests, Hex_Strings;

pragma Elaborate_All(Hex_Strings, Tests);

package RfC2202 is
   sha1_k1 : aliased constant String := (1 .. 20 => Character'Val(16#0b#));
   md5_k1  : aliased constant String := (1 .. 16 => Character'Val(16#0b#));
   d1      : aliased constant String := "Hi There";
   sha1_1  : aliased constant Hex_String := "b6173186 55057264 e28bc0b6 fb378c8e f146be00";
   md5_1   : aliased constant Hex_String := "9294727a 3638bb1c 13f48ef8 158bfc9d";

   sha1_k2 : aliased constant String := "Jefe";
   md5_k2  : String renames sha1_k2;
   d2      : aliased constant String := "what do ya want for nothing?";
   sha1_2  : aliased constant Hex_String := "effcdf6a e5eb2fa2 d27416d5 f184df9c 259a7c79";
   md5_2   : aliased constant Hex_String := "750c783e 6ab0b503 eaa86e31 0a5db738";

   sha1_k3 : aliased constant String := (1 .. 20 => Character'Val(16#aa#));
   md5_k3  : aliased constant String := (1 .. 16 => Character'Val(16#aa#));
   d3      : aliased constant String := (1 .. 50 => Character'Val(16#dd#));
   sha1_3  : aliased constant Hex_String := "125d7342 b9ac11cd 91a39af4 8aa17b4f 63f175d3";
   md5_3   : aliased constant Hex_String := "56be3452 1d144c88 dbb8c733 f0e8b3f6";

   sha1_k4 : aliased constant String := To_Data("0102030405060708090a0b0c0d0e0f10111213141516171819");
   md5_k4  : String renames sha1_k4;
   d4      : aliased constant String := (1 .. 50 => Character'Val(16#cd#));
   sha1_4  : aliased constant Hex_String := "4c9007f4 026250c6 bc8414f9 bf50c86c 2d7235da";
   md5_4   : aliased constant Hex_String := "697eaf0a ca3a3aea 3a751647 46ffaa79";
   
   sha1_k5 : aliased constant String := (1 .. 20 => Character'Val(16#0c#));
   md5_k5  : aliased constant String := (1 .. 16 => Character'Val(16#0c#));
   d5      : aliased constant String := "Test With Truncation";
   sha1_5  : aliased constant Hex_String := "4c1a0342 4b55e07f e7f27be1 d58bb932 4a9a5a04";
   md5_5   : aliased constant Hex_String := "56461ef2 342edc00 f9bab995 690efd4c";
   
   sha1_k6 : aliased constant String := (1 .. 80 => Character'Val(16#aa#));
   md5_k6  : String renames sha1_k6;
   d6      : aliased constant String := "Test Using Larger Than Block-Size Key - Hash Key First";
   sha1_6  : aliased constant Hex_String := "aa4ae5e1 5272d00e 95705637 ce8a3b55 ed402112";
   md5_6   : aliased constant Hex_String := "6b1ab7fe 4bd7bf8f 0b62e6ce 61b9d0cd";

   sha1_k7 : String renames sha1_k6;
   md5_k7  : String renames sha1_k7;
   d7      : aliased constant String := "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
   sha1_7  : aliased constant Hex_String := "e8e99d0f 45237d78 6d6bbaa7 965c7808 bbff1a91";
   md5_7   : aliased constant Hex_String := "6f630fad 67cda0ee 1fb1f562 db3aa53e";

   sha1_keys : constant Constant_String_Array := (
     sha1_k1'Access, sha1_k2'Access, sha1_k3'Access, sha1_k4'Access,
     sha1_k5'Access, sha1_k6'Access, sha1_k7'Access
   );

   sha1_data : constant Constant_String_Array := (
     d1'Access, d2'Access, d3'Access, d4'Access,
     d5'Access, d6'Access, d7'Access
   );

   sha1_hashes : constant Constant_Hex_String_Array := (
     sha1_1'Access, sha1_2'Access, sha1_3'Access, sha1_4'Access,
     sha1_5'Access, sha1_6'Access, sha1_7'Access
   );

   md5_keys : constant Constant_String_Array := (
     md5_k1'Access, md5_k2'Access, md5_k3'Access, md5_k4'Access,
     md5_k5'Access, md5_k6'Access, md5_k7'Access
   );

   md5_data : Constant_String_Array renames sha1_data;

   md5_hashes : constant Constant_Hex_String_Array := (
     md5_1'Access, md5_2'Access, md5_3'Access, md5_4'Access,
     md5_5'Access, md5_6'Access, md5_7'Access
   );
end RfC2202;



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-11-11  9:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-10 16:49 Question about Ada and constant Arrays Stephane Richard
2003-11-10 17:07 ` Preben Randhol
2003-11-10 17:10   ` Stephane Richard
2003-11-10 17:13     ` Preben Randhol
2003-11-10 17:11   ` Preben Randhol
2003-11-11  5:41   ` Nick Roberts
2003-11-11  9:34 ` Lutz Donnerhacke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox