comp.lang.ada
 help / color / mirror / Atom feed
* Length & Last Attributes.
@ 1998-10-12  0:00 BARDIN Marc
  1998-10-12  0:00 ` Steve Doiel
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: BARDIN Marc @ 1998-10-12  0:00 UTC (permalink / raw)


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

    How to get corrects values for Length & Last attributes for a string
AFTER it has been defined in the program ?
In the following example program, these 2  attributes aren't corrects after
Max_Descripteur value is known.

Anticipated thanks.
Marc


with Text_Io; use Text_Io;
procedure Test_Tri_Fichier is

   Max_Descripteur : Positive;
   subtype Ch_L_Cha�ne is String (1..Max_Descripteur);
   Fin_Sc : Positive range Ch_L_Cha�ne'range;
   package Ent_Es is new Text_Io.Integer_Io (Integer);

begin
   Max_Descripteur := 5;
   Put (" Length : "); Ent_Es.Put (Ch_L_Cha�ne'Length, 5);
   Fin_Sc := Ch_L_Cha�ne'Last;
   Put (" Fin_Sc : "); Ent_Es.Put (Fin_Sc, 5);
end Test_Tri_Fichier;






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

* Re: Length & Last Attributes.
  1998-10-12  0:00 Length & Last Attributes BARDIN Marc
@ 1998-10-12  0:00 ` Steve Doiel
  1998-10-13  0:00 ` Dale Stanbrough
  1998-10-13  0:00 ` Matthew Heaney
  2 siblings, 0 replies; 4+ messages in thread
From: Steve Doiel @ 1998-10-12  0:00 UTC (permalink / raw)


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

In your code you have the two lines:

>   Max_Descripteur : Positive;
>   subtype Ch_L_Cha�ne is String (1..Max_Descripteur);

Which are "elaborated" when the program begins.  At this time and ONLY at
this time is the value of Max_escripteur used to define the size of the
array.  Since you have not defined a value for Max_Descripteur when the type
Ch_L_Cha�ne is defined the actual size of the array is unpredictable, but
likely zero.

The functionality you are looking for may be found in either of the two
packages:

  Ada.Strings.Bounded

or

  Ada.Strings.Unbounded


I hope this helps,
SteveD






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

* Re: Length & Last Attributes.
  1998-10-12  0:00 Length & Last Attributes BARDIN Marc
  1998-10-12  0:00 ` Steve Doiel
  1998-10-13  0:00 ` Dale Stanbrough
@ 1998-10-13  0:00 ` Matthew Heaney
  2 siblings, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1998-10-13  0:00 UTC (permalink / raw)


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

"BARDIN Marc" <Marc.Bardin@wanadoo.fr> writes:

>     How to get corrects values for Length & Last attributes for a string
> AFTER it has been defined in the program ?
> In the following example program, these 2  attributes aren't corrects after
> Max_Descripteur value is known.

procedure Op (S : in String) is
begin
   ... S'First ...
   ... S'Last ...
   ... S'Length ...


> 
> Anticipated thanks.
> Marc
> 
> 
> with Text_Io; use Text_Io;
> procedure Test_Tri_Fichier is
> 
>    Max_Descripteur : Positive;
>    subtype Ch_L_Cha�ne is String (1..Max_Descripteur);

The rule is "linear elaboration."  The problem is that you elaborate the
subtype Ch_L_Chane using object Max_Descipteur, which hasn't been given
a value!  

So your subtype will have some value for T'Last, but who knows what it
will be?

The solution is to elaborate the subtype AFTER giving Max_Descripteur a
value.


>    Fin_Sc : Positive range Ch_L_Cha�ne'range;
>    package Ent_Es is new Text_Io.Integer_Io (Integer);
> 
> begin
>    Max_Descripteur := 5;

Now declare your subtype:

   declare
      subtype Ch_L_Chane is String (1 .. Max_Descripteur);
   begin
      ... Ch_L_Chane'Length ...
      ... Ch_L_Chane'Last ...      

      

>    Put (" Length : "); Ent_Es.Put (Ch_L_Cha�ne'Length, 5);
>    Fin_Sc := Ch_L_Cha�ne'Last;
>    Put (" Fin_Sc : "); Ent_Es.Put (Fin_Sc, 5);
> end Test_Tri_Fichier;




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

* Re: Length & Last Attributes.
  1998-10-12  0:00 Length & Last Attributes BARDIN Marc
  1998-10-12  0:00 ` Steve Doiel
@ 1998-10-13  0:00 ` Dale Stanbrough
  1998-10-13  0:00 ` Matthew Heaney
  2 siblings, 0 replies; 4+ messages in thread
From: Dale Stanbrough @ 1998-10-13  0:00 UTC (permalink / raw)


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

BARDIN Marc wrote:

 with Text_Io; use Text_Io;
 procedure Test_Tri_Fichier is
 
    Max_Descripteur : Positive;
    subtype Ch_L_Cha�ne is String (1..Max_Descripteur);
    Fin_Sc : Positive range Ch_L_Cha�ne'range;
    package Ent_Es is new Text_Io.Integer_Io (Integer);
 
 begin
    Max_Descripteur := 5;
    Put (" Length : "); Ent_Es.Put (Ch_L_Cha�ne'Length, 5);
    Fin_Sc := Ch_L_Cha�ne'Last;
    Put (" Fin_Sc : "); Ent_Es.Put (Fin_Sc, 5);
 end Test_Tri_Fichier;


No, sorry, you can't do this in Ada in this way. More than likely
the better solution is to use Unbounded_Strings, found in package
Ada.Strings.Unbounded (for some strange reason, this wonderfully
useful type never seems to be mentioned in introductory text books,
despite the amazing simplicity it brings to the "string" model, esp.
for beginning students).



Dale




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

end of thread, other threads:[~1998-10-13  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-10-12  0:00 Length & Last Attributes BARDIN Marc
1998-10-12  0:00 ` Steve Doiel
1998-10-13  0:00 ` Dale Stanbrough
1998-10-13  0:00 ` Matthew Heaney

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