comp.lang.ada
 help / color / mirror / Atom feed
* How to initiate part of a string?
@ 1998-08-24  0:00 Hans Marqvardsen
  1998-08-25  0:00 ` Tom Moran
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Hans Marqvardsen @ 1998-08-24  0:00 UTC (permalink / raw)


I have a package GLOBALS, 
containing global types and variables, 
(but no procedures or functions).

	package Globals is --among other things
		Txt: string (1..100);
	end Globals;

Is there a clean way to specify an initial value for a slice of Txt ?


	
Sincerely,
Hans Marqvardsen, hm@ddre.dk.




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

* Re: How to initiate part of a string?
  1998-08-24  0:00 How to initiate part of a string? Hans Marqvardsen
@ 1998-08-25  0:00 ` Tom Moran
  1998-08-25  0:00   ` Samuel Tardieu
  1998-08-25  0:00 ` Mark A Biggar
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 16+ messages in thread
From: Tom Moran @ 1998-08-25  0:00 UTC (permalink / raw)


>Txt: string (1..100);
>Is there a clean way to specify an initial value for a slice of Txt ?
Do you mean
  Txt : String(1 .. 100) := (1 .. 27=>' ', "abcd",31..100=>' '):





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

* Re: How to initiate part of a string?
  1998-08-24  0:00 How to initiate part of a string? Hans Marqvardsen
  1998-08-25  0:00 ` Tom Moran
@ 1998-08-25  0:00 ` Mark A Biggar
  1998-08-26  0:00 ` Mats Weber
  1998-08-26  0:00 ` Frank Ecke
  3 siblings, 0 replies; 16+ messages in thread
From: Mark A Biggar @ 1998-08-25  0:00 UTC (permalink / raw)


Hans Marqvardsen wrote:

> I have a package GLOBALS,
> containing global types and variables,
> (but no procedures or functions).
> 
>         package Globals is --among other things
>                 Txt: string (1..100);
>         end Globals;
> 
> Is there a clean way to specify an initial value for a slice of Txt ?


If you realy intend that the rest of the string be undefined, then you can
always do it in a package body like so:

package body Globals is
begin
	Txt(1..6) := "abcdef";
end Globals;

You will also need to add a pragma Elabotate_Body; to the package
spec to keep the compiler happy (Ada 95).

--

Mark Biggar
mark.a.biggar@lmco.com




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

* Re: How to initiate part of a string?
  1998-08-25  0:00 ` Tom Moran
@ 1998-08-25  0:00   ` Samuel Tardieu
  1998-08-26  0:00     ` Tom Moran
  0 siblings, 1 reply; 16+ messages in thread
From: Samuel Tardieu @ 1998-08-25  0:00 UTC (permalink / raw)


Tom> Do you mean
Tom>
Tom>   Txt : String(1 .. 100) := (1 .. 27=>' ', "abcd",31..100=>' '):

This is not valid Ada.

One way to do it is (as Mark wrote) is to put a pragma Elaborate_Body
in the spec and initialize that part of the string in the body
elaboration.

Put I wouldn't have said "use pragma Elaborate_Body to keep the
compiler happy", but instead "use pragma Elaborate_Body so that you
know that the string have been initialized when you're using it from
another package".

  Sam
-- 
Samuel Tardieu -- sam@ada.eu.org




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

* Re: How to initiate part of a string?
  1998-08-25  0:00   ` Samuel Tardieu
@ 1998-08-26  0:00     ` Tom Moran
  1998-08-26  0:00       ` Robert I. Eachus
  1998-09-19  0:00       ` dewarr
  0 siblings, 2 replies; 16+ messages in thread
From: Tom Moran @ 1998-08-26  0:00 UTC (permalink / raw)


>Tom>   Txt : String(1 .. 100) := (1 .. 27=>' ', "abcd",31..100=>' '):

>This is not valid Ada.
Quite so, it should have been
  Txt : String(1 .. 100) := (1 ..  27=>' ') & "abcd" & (32 .. 100=>'
');




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

* Re: How to initiate part of a string?
  1998-08-24  0:00 How to initiate part of a string? Hans Marqvardsen
                   ` (2 preceding siblings ...)
  1998-08-26  0:00 ` Mats Weber
@ 1998-08-26  0:00 ` Frank Ecke
  1998-08-26  0:00   ` Robert I. Eachus
  3 siblings, 1 reply; 16+ messages in thread
From: Frank Ecke @ 1998-08-26  0:00 UTC (permalink / raw)


On Mon, 24 Aug 1998 14:59:07 +0100, Hans Marqvardsen <hm@ddre.dk> wrote:

> Is there a clean way to specify an initial value for a slice of Txt ?


After with'ing and use'ing Ada.Strings and Ada.Strings.Fixed,

   Txt : String(1 .. 100) := Replace_Slice(100 * Space, 50, 54, "Hello");

should do the trick.


Hope this helps.



Regards,

Frank




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

* Re: How to initiate part of a string?
  1998-08-24  0:00 How to initiate part of a string? Hans Marqvardsen
  1998-08-25  0:00 ` Tom Moran
  1998-08-25  0:00 ` Mark A Biggar
@ 1998-08-26  0:00 ` Mats Weber
  1998-08-26  0:00   ` Frank Ecke
  1998-08-26  0:00 ` Frank Ecke
  3 siblings, 1 reply; 16+ messages in thread
From: Mats Weber @ 1998-08-26  0:00 UTC (permalink / raw)


Hans Marqvardsen wrote:
> 
> I have a package GLOBALS,
> containing global types and variables,
> (but no procedures or functions).
> 
>         package Globals is --among other things
>                 Txt: string (1..100);
>         end Globals;
> 
> Is there a clean way to specify an initial value for a slice of Txt ?

If the value, and especially the length, of that string is going to change,
then String(1 .. 100) is a very bad type to use. Instead, you should use one
of the varying length string packages Ada.Strings.Bounded and Ada.Strings.Unbounded.

Example:

with Ada.Strings.Unbounded;

use Ada.Strings.Unbounded;

package Globals is
   Txt : Unbounded_String := To_Unbounded_String("bla bla bla");
end Globals;

This way, you need no package body, and you can easily change the value if Txt
later on.




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

* Re: How to initiate part of a string?
  1998-08-26  0:00 ` Mats Weber
@ 1998-08-26  0:00   ` Frank Ecke
  0 siblings, 0 replies; 16+ messages in thread
From: Frank Ecke @ 1998-08-26  0:00 UTC (permalink / raw)


On Wed, 26 Aug 1998 16:43:09, Mats Weber <Mats.Weber@elca-matrix.ch> wrote:

>If the value, and especially the length, of that string is going to change,
>then String(1 .. 100) is a very bad type to use. Instead, you should use one
>of the varying length string packages Ada.Strings.Bounded and Ada.Strings.
>Unbounded.
>
>Example:
>
>with Ada.Strings.Unbounded;
>
>use Ada.Strings.Unbounded;
>
>package Globals is
>   Txt : Unbounded_String := To_Unbounded_String("bla bla bla");
>end Globals;
>
B
>This way, you need no package body, and you can easily change the value if Txt
>later on.

Of course, unbounded strings provide more flexibility but I think Hans'
intention was to provide an initial value for a *slice* of Txt.  Your proposal
seems to only initialize a ``special slice'' of Txt, namely
Txt(1 .. Length("bla bla bla")).

Using unbounded strings, I suggest:

Txt : Unbounded_String :=
  Replace_Slice(100 * Space, 50, 54, "Hello");


or, as suggested by Hans:

Txt : Unbounded_String :=
  Overwrite(100 * Space, 5, "No need to count");


Regards,

Frank




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

* Re: How to initiate part of a string?
  1998-08-26  0:00     ` Tom Moran
@ 1998-08-26  0:00       ` Robert I. Eachus
  1998-08-26  0:00         ` Tom Moran
  1998-09-19  0:00       ` dewarr
  1 sibling, 1 reply; 16+ messages in thread
From: Robert I. Eachus @ 1998-08-26  0:00 UTC (permalink / raw)


In article <35e3675e.37587430@SantaClara01.news.InterNex.Net> tmoran@bix.com (Tom Moran) writes:

   Quite so, it should have been
     Txt : String(1 .. 100) := (1 ..  27=>' ') & "abcd" & (32 .. 100=>' ');

    That actually is not the same thing.  The bounds of "abcd" are
1..4, but the catenation preserves the lower bound of the leftmost
entry.  So:

    Txt : String(1 .. 100) := (1 ..  27=>' ') & "abcd" & (1 .. 69=>' ');

is equivalent, except for expository purposes.  To create an aggregate
that can initialize the string write:

    Txt2: String(1..100) := (1..27 => ' ', 28 => 'a', 29 => 'b',
                             30 => 'c', 31 => 'd', 32..100 => ' ');

Painful, but at least in this case you get better bounds info.  Better
still try:

    Txt3: String(1..100) := (28 => 'a', 29 => 'b', 30 => 'c', 31 => 'd',
                             others => ' ');

Of course, reality is that if you really need to do something like
this, you would, I hope, use the standard libraries:

    with Ada.Strings.Fixed;use Ada.Strings.Fixed;
    ...
    Txt4: String(1..100) := Overwrite(Source => String'(1..100 => ' '),
                                      Position => 28,
                                      New_Item => "abcd");

Now finally, I can answer the original question.  Note that Source
argument above was provided as an aggregate, I could also have
written, using the "*" function in Ada.Strings.Fixed:

    Txt5: String(1..100) := Overwrite(Source => 100 * ' ',
                                      Position => 28,
                                      New_Item => "abcd");

or if you really insist on partial initialization:

    Junk: String(1..100); -- uninitialized
    Txt6: String(1..100) := Overwrite(Source => Junk,
                                      Position => 28,
                                      New_Item => "abcd");


--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




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

* Re: How to initiate part of a string?
  1998-08-26  0:00 ` Frank Ecke
@ 1998-08-26  0:00   ` Robert I. Eachus
  0 siblings, 0 replies; 16+ messages in thread
From: Robert I. Eachus @ 1998-08-26  0:00 UTC (permalink / raw)


In article <slrn6u7ah3.9v3.franke@paxp01.mipool.uni-jena.de> franke@minet.uni-jena.de (Frank Ecke) writes:

 > After with'ing and use'ing Ada.Strings and Ada.Strings.Fixed,

 >    Txt : String(1 .. 100) := Replace_Slice(100 * Space, 50, 54, "Hello");

 > should do the trick.

   This will work, but Replace_Slice does more than intended, which is
why I recommend Overwrite.  (It is also likely that Overwrite will be
faster.)  Overwrite returns a string of the same length as the first
argument, but in Replace_Slice, the slice replaced does not need to be
the same length as the last argument. For example, 

   Txt : String(1 .. 100) := Replace_Slice(95 * Space, 50, 0, "Hello");

   will return the same string. And:

   Replace_Slice("Robert I. Eachus", 8, 9, "Iredell") replaces my
middle initial with my middle name.
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




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

* Re: How to initiate part of a string?
  1998-08-26  0:00       ` Robert I. Eachus
@ 1998-08-26  0:00         ` Tom Moran
  1998-09-24  0:00           ` Bob Fletcher
  0 siblings, 1 reply; 16+ messages in thread
From: Tom Moran @ 1998-08-26  0:00 UTC (permalink / raw)


>  Txt : String(1 .. 100) := (1 ..  27=>' ') & "abcd" & (32 .. 100=>' ');
vs
>    Txt : String(1 .. 100) := (1 ..  27=>' ') & "abcd" & (1 .. 69=>' ');
The former has the advantage of less error-prone arithmetic required
of the writer or readers of the code.




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

* Re: How to initiate part of a string?
  1998-08-26  0:00     ` Tom Moran
  1998-08-26  0:00       ` Robert I. Eachus
@ 1998-09-19  0:00       ` dewarr
  1 sibling, 0 replies; 16+ messages in thread
From: dewarr @ 1998-09-19  0:00 UTC (permalink / raw)


In article <35e3675e.37587430@SantaClara01.news.InterNex.Net>,
  tmoran@bix.com (Tom Moran) wrote:
> >Tom>   Txt : String(1 .. 100) := (1 .. 27=>' ', "abcd",31..100=>' '):
>
> >This is not valid Ada.
> Quite so, it should have been
>   Txt : String(1 .. 100) := (1 ..  27=>' ') & "abcd" & (32 .. 100=>'
> ');


By the way, I am not sure I have seen this pointed out, but
the subject line of this thread is quite wrong.

You *initiate* a process
You *initialize* data

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

* Re: How to initiate part of a string?
  1998-08-26  0:00         ` Tom Moran
@ 1998-09-24  0:00           ` Bob Fletcher
  1998-09-24  0:00             ` Tom Moran
  0 siblings, 1 reply; 16+ messages in thread
From: Bob Fletcher @ 1998-09-24  0:00 UTC (permalink / raw)


can't you do:

Txt: String(1..100) := (28..31=>"abcd", Others=>' ');

or am I missing the point;

Tom Moran <tmoran@bix.com> wrote in article
<35e45c89.12901035@SantaClara01.news.InterNex.Net>...
> >  Txt : String(1 .. 100) := (1 ..  27=>' ') & "abcd" & (32 .. 100=>' ');
> vs
> >    Txt : String(1 .. 100) := (1 ..  27=>' ') & "abcd" & (1 .. 69=>' ');
> The former has the advantage of less error-prone arithmetic required
> of the writer or readers of the code.
> 




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

* Re: How to initiate part of a string?
  1998-09-24  0:00           ` Bob Fletcher
@ 1998-09-24  0:00             ` Tom Moran
  1998-09-25  0:00               ` dewarr
  0 siblings, 1 reply; 16+ messages in thread
From: Tom Moran @ 1998-09-24  0:00 UTC (permalink / raw)


>Txt: String(1..100) := (28..31=>"abcd", Others=>' ');
It looks nice, but it doesn't compile.




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

* Re: How to initiate part of a string?
  1998-09-24  0:00             ` Tom Moran
@ 1998-09-25  0:00               ` dewarr
  1998-09-29  0:00                 ` Aidan Skinner
  0 siblings, 1 reply; 16+ messages in thread
From: dewarr @ 1998-09-25  0:00 UTC (permalink / raw)


In article <360a8549.265252@SantaClara01.news.InterNex.Net>,
  tmoran@bix.com (Tom Moran) wrote:
> >Txt: String(1..100) := (28..31=>"abcd", Others=>' ');
> It looks nice, but it doesn't compile.


Clearly this is illegal, compare with the obviously legal

   Txt: String(1..100) := (28..31=>'x', Others=>' ');

Clearly these cannot both be type correct. If you expect
the top one to compile, you don't have a precise view of
what is going on semantically.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

* Re: How to initiate part of a string?
  1998-09-25  0:00               ` dewarr
@ 1998-09-29  0:00                 ` Aidan Skinner
  0 siblings, 0 replies; 16+ messages in thread
From: Aidan Skinner @ 1998-09-29  0:00 UTC (permalink / raw)


On Fri, 25 Sep 1998 02:32:14 GMT, dewarr@my-dejanews.com 
<dewarr@my-dejanews.com> wrote:

>In article <360a8549.265252@SantaClara01.news.InterNex.Net>,
>  tmoran@bix.com (Tom Moran) wrote:
>> >Txt: String(1..100) := (28..31=>"abcd", Others=>' ');
>> It looks nice, but it doesn't compile.
>
>Clearly this is illegal, compare with the obviously legal
>
>   Txt: String(1..100) := (28..31=>'x', Others=>' ');
>
>Clearly these cannot both be type correct. If you expect
>the top one to compile, you don't have a precise view of
>what is going on semantically.

Would 
	Txt: String(1..100) := (28..31 => 'a','b','c','d', Others => ' ');

be a good compromise in this situation? it looks nice and keeps the
same semantics. It then opens the possibility of

	Txt: string(1..100) := (28..31 => ('a'..'d'), Others => ' ');

- Aidan
-- 
HMFC - Scottish Cup Winners 1998
http://www.skinner.demon.co.uk/aidan
http://www.gla.ac.uk/Clubs/WebSoc/~974075s/
"I don't patronise bunny rabbits"




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

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

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-08-24  0:00 How to initiate part of a string? Hans Marqvardsen
1998-08-25  0:00 ` Tom Moran
1998-08-25  0:00   ` Samuel Tardieu
1998-08-26  0:00     ` Tom Moran
1998-08-26  0:00       ` Robert I. Eachus
1998-08-26  0:00         ` Tom Moran
1998-09-24  0:00           ` Bob Fletcher
1998-09-24  0:00             ` Tom Moran
1998-09-25  0:00               ` dewarr
1998-09-29  0:00                 ` Aidan Skinner
1998-09-19  0:00       ` dewarr
1998-08-25  0:00 ` Mark A Biggar
1998-08-26  0:00 ` Mats Weber
1998-08-26  0:00   ` Frank Ecke
1998-08-26  0:00 ` Frank Ecke
1998-08-26  0:00   ` Robert I. Eachus

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