comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Concatenation and Characters
Date: Thu, 10 Oct 2002 17:45:42 GMT
Date: 2002-10-10T17:45:42+00:00	[thread overview]
Message-ID: <wccsmzep4qh.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 12hp9.796$_u6.380@nwrddc01.gnilink.net

"Justin Birtwell" <jbirtwell@yahoo.com> writes:

> This is what I don't understand!  How can you declare a string variable when
> you don't know before hand what the string is going to be?

Type String is fixed-length, meaning that you have to know the exact
length when you create a String object.  (But different Strings can be
different lengths.)  Yes, this is somewhat restrictive.

One thing you can do is leave the bounds off.  That's allowed if the
String has an initial value, and the String is then exactly as long as
that value (and its length never changes).  Like this:

    S: String := "ABCD";
    T: String := "EF";
    U: String := S & T; -- U has bounds 1..6.

Note that you can write functions that return "String" -- the length of
the String is determined when the function returns, and can be different
each time it's called.  And you can use a call to initialize a variable
or constant:

    X: constant String := Func(...);

In fact, "&" is really just a built-in function that works this way.

If you really want strings whose length changes over time, you should
use the Bounded or Unbounded_Strings packages.  Unbounded_Strings is the
most flexible.

Your other question was:

> Chars:array range(1..3) of Characters:=('a','b','c');
> 
> How can I convert it to a string?
> 
> Put(Chars) --Compile Error:Invalid param list!

You can use a type conversion, as in:

    Put(String(Chars));

There are lots of complicated rules about what types may be converted to
what other types -- look up "type conversion" in the RM or some
textbook.

By the way, you could have used a string_literal above:

    Chars: array (1..3) of Character := "abc";

String_literals are not specific to type String; they work for any
"string type".  A string type is any one-dimensional array of a
character type.  A character type is any enumeration type that has
character literals -- although you normally only need the predefined
ones Character and Wide_Character.  The (anonymous) type of Chars is a
string type.

Or, you could consider whether you really want Chars to have a separate
type -- you could say:

    Chars: String := "abc";

or

    Chars: constant String := "abc";

if Chars never changes.  Then you can "Put(Chars);".

- Bob



  parent reply	other threads:[~2002-10-10 17:45 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-10 14:50 Concatenation and Characters Justin Birtwell
2002-10-10 14:55 ` Preben Randhol
2002-10-10 15:04   ` Justin Birtwell
2002-10-10 15:22     ` Preben Randhol
2002-10-10 15:30       ` Justin Birtwell
2002-10-10 16:05         ` Georg Bauhaus
2002-10-10 16:07         ` Preben Randhol
2002-10-10 17:45         ` Robert A Duff [this message]
2002-10-10 15:32       ` Justin Birtwell
2002-10-10 15:36         ` Preben Randhol
2002-10-10 16:44         ` Mark Biggar
2002-10-10 17:45           ` Stephen Leake
2002-10-10 21:53             ` Mark Biggar
2002-10-18 17:03           ` Programmer Dude
2002-10-18 18:13             ` Preben Randhol
2002-10-18 18:36             ` Wes Groleau
2002-10-21 15:16               ` Georg Bauhaus
2002-10-18 21:33             ` Mark Biggar
2002-10-20  2:01               ` Dmitry A.Kazakov
2002-10-21 14:13                 ` Wes Groleau
2002-10-21 15:22                   ` Dmitry A. Kazakov
2002-10-21 19:38                     ` Georg Bauhaus
2002-10-22 22:15                       ` Dmitry A.Kazakov
2002-10-22 12:05                         ` Georg Bauhaus
2002-10-22 12:19                           ` Lutz Donnerhacke
2002-10-22 14:43                             ` Georg Bauhaus
2002-10-23  8:39                           ` Dmitry A. Kazakov
2002-10-23 14:39                             ` Georg Bauhaus
2002-10-24  8:18                               ` Dmitry A. Kazakov
2002-10-21 16:50                   ` Warren W. Gay VE3WWG
2002-10-21 15:20             ` Georg Bauhaus
2002-10-21 17:51               ` Programmer Dude
2002-10-21 18:48                 ` Jim Rogers
2002-10-21 19:44                   ` tmoran
2002-10-21 20:42                   ` Programmer Dude
2002-10-22  1:42                     ` Jeffrey Carter
2002-10-22 14:37                       ` Robert A Duff
2002-10-22 18:51                         ` Jeffrey Carter
2002-10-23  7:01                         ` Pascal Obry
2002-10-22 14:45                       ` Matthew Heaney
2002-10-22 18:47                         ` Jeffrey Carter
2002-10-22 21:31                         ` Robert A Duff
     [not found]                         ` <3DB59D75.20609 <wccd6q29n3l.fsf@shell01.TheWorld.com>
2002-10-23  2:02                           ` Jeffrey Carter
2002-10-23 13:16                             ` Matthew Heaney
2002-10-23 19:11                               ` Jeffrey Carter
2002-10-23 15:24                             ` Robert A Duff
2002-10-23 19:24                               ` Jeffrey Carter
2002-10-24  0:33                                 ` Robert A Duff
2002-10-22  3:46                     ` Jim Rogers
2002-10-22 14:48                       ` Robert A Duff
2002-10-22 15:02                         ` Fraser Wilson
2002-10-22 15:38                           ` David C. Hoos
2002-10-22 15:44                             ` Fraser Wilson
2002-10-22 16:13                         ` Robert A Duff
2002-10-23  8:58                           ` Dmitry A. Kazakov
2002-10-23  9:08                             ` Lutz Donnerhacke
2002-10-23  9:34                               ` Dmitry A. Kazakov
2002-10-23 10:10                                 ` Lutz Donnerhacke
2002-10-23 17:15                                 ` Frank J. Lhota
2002-10-24  8:41                                   ` Dmitry A. Kazakov
2002-10-24  9:25                                   ` Fraser Wilson
2002-10-24 14:13                                     ` Matthew Heaney
     [not found]                         ` <un <wcc7kgazc20.fsf@shell01.TheWorld.com>
2002-10-22 16:46                           ` David C. Hoos
2002-10-22  8:51                   ` Stuart Palin
2002-10-22 18:56                     ` Programmer Dude
2002-10-21 19:42                 ` Georg Bauhaus
  -- strict thread matches above, loose matches on Subject: below --
2002-10-11  5:04 Grein, Christoph
2002-10-11 10:30 ` Preben Randhol
2002-10-23  5:15 Grein, Christoph
2002-10-23 13:19 ` Matthew Heaney
2002-10-24  5:53 Grein, Christoph
2002-10-24 14:04 ` Matthew Heaney
replies disabled

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