comp.lang.ada
 help / color / mirror / Atom feed
* String Length
@ 1998-10-01  0:00 mincus
  1998-10-02  0:00 ` Pascal Obry
  0 siblings, 1 reply; 14+ messages in thread
From: mincus @ 1998-10-01  0:00 UTC (permalink / raw)


If anyone knows how to determine the size of a string I would be very
grateful.

I thought that I could do a

string'last

and that would return the position of the last char, but I just get an
error, please help

mincus






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

* Re: String Length
  1998-10-01  0:00 String Length mincus
@ 1998-10-02  0:00 ` Pascal Obry
  1998-10-02  0:00   ` Ehud Lamm
  0 siblings, 1 reply; 14+ messages in thread
From: Pascal Obry @ 1998-10-02  0:00 UTC (permalink / raw)


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


mincus a �crit dans le message <36140400.0@news.cyberenet.net>...
>If anyone knows how to determine the size of a string I would be very
>grateful.
>
>I thought that I could do a
>
>string'last
>
>and that would return the position of the last char, but I just get an
>error, please help
>
>mincus
>
>

Given a string like :

    Message : String (2 .. 4) := "abc";

Message'First = 2
Message'Last = 4
Message'Lenght = 3 --  this is the size of the string

Pascal.







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

* Re: String Length
  1998-10-02  0:00 ` Pascal Obry
@ 1998-10-02  0:00   ` Ehud Lamm
  0 siblings, 0 replies; 14+ messages in thread
From: Ehud Lamm @ 1998-10-02  0:00 UTC (permalink / raw)


First let me give two approaches that work:

--  CODE FOLLOWS
with ada.strings.unbounded; use ada.strings.unbounded;
procedure try_strings is
   
  s:string := "ehud lamm";
begin

   put(s'length);
   put(length(to_unbounded_string("ehud")));
   
    
end;
-- END OF CODE

There are huge diffrences between the two methods used above. The second
way (converting to an unbounded string) is costlier in any respect. I urge
you to understand this.

The first solution, using the attribute 'length on a String type variable
is nice, since it results in a static expression. One confusing detail may
be, that since String itself is an indefinite type (it is basically an
unconstraint array type), you can not do something like this:

-- WARNNING BAD CODE AHEAD
s:String
i:iinteger;
begin
  s:="ehud";
  i:=s'length;
end;
-- SAFE AGAIN

You simply can not declare variables of type string without contraining it
to some fixed length. When you say s:string:="ehud", the complier knows
the length needed for s, by looking at the initialization.

One final note: You can not say: "ehud"'length. This attribute will not
work on a value, it wants a variable.


Hope this helps!

Ehud Lamm     mslamm@pluto.mscc.huji.ac.il





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

* Re: STRING length
  2006-11-14 22:51 STRING length markww
@ 2006-11-14 22:24 ` Georg Bauhaus
  2006-11-15  0:44   ` markww
  2006-11-15  1:27 ` Jeffrey R. Carter
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 14+ messages in thread
From: Georg Bauhaus @ 2006-11-14 22:24 UTC (permalink / raw)


On Tue, 2006-11-14 at 14:51 -0800, markww wrote:
> Hi,
> 
> How does one use a variable length string in ada?

You use variable length strings in Ada by declaring them
to be of type UNBOUNDED_STRING which is defined in
Ada.Strings.Unbounded. 

type MY_RECORD is
  record
    Name: UNBOUNDED_STRING;
    Phone: UNBOUNDED_STRING;
    Address: UNBOUNDED_STRING;
  end record;

Given that Phone is likely to be limited in length, you
could consider declaring the Phone component to be of
type BOUNDED_STRING, which is a string type with a maximum length.
Unlike STRING, objects of this type can have any number
of characters up to the maximum. See Ada.Strings.Bounded.

Yet another use of strings is in nested scopes: If you need
a string in just one place, e.g. temporarily, you can use
a plain STRING as in

  declare
    temp: constant STRING := some_string_returning_func(...);
  begin
    -- use temp
  end;

The point here is that the `temp` string variable takes
its bound from the initialization. You can also make it a
variable, if you need to write to string components.

See
http://en.wikibooks.org/wiki/Ada_Programming/Strings


-- Georg 





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

* STRING length
@ 2006-11-14 22:51 markww
  2006-11-14 22:24 ` Georg Bauhaus
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: markww @ 2006-11-14 22:51 UTC (permalink / raw)


Hi,

How does one use a variable length string in ada? I created a record
like:

    type MY_RECORD is
      record
         m_strName     : STRING(1..5);
         m_strPhone    : STRING(1..5);
         m_strAddress  : STRING(1..5);
      end record;

However when trying to assign the members of the record, at runtime I
may get an exception if the assigned string is not exactly 5
characters. Is there a different STRING type which doesn't care how
long the assigned string is (as long as it's shorter than the max) or
which just grows to meet the assigned string length?

Thanks




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

* Re: STRING length
  2006-11-14 22:24 ` Georg Bauhaus
@ 2006-11-15  0:44   ` markww
  2006-11-15  1:09     ` markww
  2006-11-15  1:17     ` Ludovic Brenta
  0 siblings, 2 replies; 14+ messages in thread
From: markww @ 2006-11-15  0:44 UTC (permalink / raw)


Thanks Georg, that looks to be exactly what I need. I do have a problem
'#including', or, 'withing' rather unbounded string.
Now the head of my source file looks like:

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

which is alright but as soon as I try:

    use Ada.Strings.Unbounded;

the compiler gives me a bunch of errors, it seems to conflict with
Text_IO / Integer_Text_IO? Seems like all my previous calls to Put()
now became invalid. I'm not familiar with Ada but with C++ and
understand namespace collisions, is the same thing going on here?

Thanks


On Nov 14, 5:24 pm, Georg Bauhaus <bauh...@futureapps.de> wrote:
> On Tue, 2006-11-14 at 14:51 -0800, markww wrote:
> > Hi,
>
> > How does one use a variable length string in ada?You use variable length strings in Ada by declaring them
> to be of type UNBOUNDED_STRING which is defined in
> Ada.Strings.Unbounded.
>
> type MY_RECORD is
>   record
>     Name: UNBOUNDED_STRING;
>     Phone: UNBOUNDED_STRING;
>     Address: UNBOUNDED_STRING;
>   end record;
>
> Given that Phone is likely to be limited in length, you
> could consider declaring the Phone component to be of
> type BOUNDED_STRING, which is a string type with a maximum length.
> Unlike STRING, objects of this type can have any number
> of characters up to the maximum. See Ada.Strings.Bounded.
>
> Yet another use of strings is in nested scopes: If you need
> a string in just one place, e.g. temporarily, you can use
> a plain STRING as in
>
>   declare
>     temp: constant STRING := some_string_returning_func(...);
>   begin
>     -- use temp
>   end;
>
> The point here is that the `temp` string variable takes
> its bound from the initialization. You can also make it a
> variable, if you need to write to string components.
>
> Seehttp://en.wikibooks.org/wiki/Ada_Programming/Strings
> 
> -- Georg




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

* Re: STRING length
  2006-11-15  0:44   ` markww
@ 2006-11-15  1:09     ` markww
  2006-11-15  1:21       ` Ludovic Brenta
  2006-11-15  1:29       ` Jeffrey R. Carter
  2006-11-15  1:17     ` Ludovic Brenta
  1 sibling, 2 replies; 14+ messages in thread
From: markww @ 2006-11-15  1:09 UTC (permalink / raw)


Actually please disregard the previous post, I found I needed to with
and use:

    Ada.Text_IO.Unbounded_IO

So.. finally one compilation error remains, the actual call to my
function:

    begin

        Add_Record("Mark", "555-555-5555", "123 main street");

    end LinkList;

the error is:

    expected private type "Ada.Strings.Unbounded.Unbounded_String"

what does that mean? The problem is with calling the procedure, if I
comment the call out compilation is successful. The procedure looks
like:

    procedure Add_Record(strName : in UNBOUNDED_STRING;
                                      strPhone : in UNBOUNDED_STRING;
                                      strAddress : in UNBOUNDED_STRING)
is
       Temp : CHAR_REC_POINT;
   begin
	Put(strName);
	New_Line;
	Put(strPhone);
	New_Line;
	Put(strAddress);
	New_Line;
    end Add_Record;

I guess the compiler doesn't interpret a literal string as an
unbounded_string?

Thanks,
Mark


On Nov 14, 7:44 pm, "markww" <mar...@gmail.com> wrote:
> Thanks Georg, that looks to be exactly what I need. I do have a problem
> '#including', or, 'withing' rather unbounded string.
> Now the head of my source file looks like:
>
>     with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Strings.Unbounded;
>     use Ada.Text_IO, Ada.Integer_Text_IO;
>
> which is alright but as soon as I try:
>
>     use Ada.Strings.Unbounded;
>
> the compiler gives me a bunch of errors, it seems to conflict with
> Text_IO / Integer_Text_IO? Seems like all my previous calls to Put()
> now became invalid. I'm not familiar with Ada but with C++ and
> understand namespace collisions, is the same thing going on here?
>
> Thanks
>
> On Nov 14, 5:24 pm, Georg Bauhaus <bauh...@futureapps.de> wrote:
>
>
>
> > On Tue, 2006-11-14 at 14:51 -0800, markww wrote:
> > > Hi,
>
> > > How does one use a variable length string in ada?You use variable length strings in Ada by declaring them
> > to be of type UNBOUNDED_STRING which is defined in
> > Ada.Strings.Unbounded.
>
> > type MY_RECORD is
> >   record
> >     Name: UNBOUNDED_STRING;
> >     Phone: UNBOUNDED_STRING;
> >     Address: UNBOUNDED_STRING;
> >   end record;
>
> > Given that Phone is likely to be limited in length, you
> > could consider declaring the Phone component to be of
> > type BOUNDED_STRING, which is a string type with a maximum length.
> > Unlike STRING, objects of this type can have any number
> > of characters up to the maximum. See Ada.Strings.Bounded.
>
> > Yet another use of strings is in nested scopes: If you need
> > a string in just one place, e.g. temporarily, you can use
> > a plain STRING as in
>
> >   declare
> >     temp: constant STRING := some_string_returning_func(...);
> >   begin
> >     -- use temp
> >   end;
>
> > The point here is that the `temp` string variable takes
> > its bound from the initialization. You can also make it a
> > variable, if you need to write to string components.
>
> > Seehttp://en.wikibooks.org/wiki/Ada_Programming/Strings
> 
> > -- Georg- Hide quoted text -- Show quoted text -




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

* Re: STRING length
  2006-11-15  0:44   ` markww
  2006-11-15  1:09     ` markww
@ 2006-11-15  1:17     ` Ludovic Brenta
  1 sibling, 0 replies; 14+ messages in thread
From: Ludovic Brenta @ 2006-11-15  1:17 UTC (permalink / raw)


markww writes:
> Thanks Georg, that looks to be exactly what I need. I do have a problem
> '#including', or, 'withing' rather unbounded string.
> Now the head of my source file looks like:
>
>     with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Strings.Unbounded;
>     use Ada.Text_IO, Ada.Integer_Text_IO;
>
> which is alright but as soon as I try:
>
>     use Ada.Strings.Unbounded;
>
> the compiler gives me a bunch of errors, it seems to conflict with
> Text_IO / Integer_Text_IO? Seems like all my previous calls to Put()
> now became invalid. I'm not familiar with Ada but with C++ and
> understand namespace collisions, is the same thing going on here?

In theory, a use clause may make subprogram calls ambiguous and thus
illegal, but I don't think that's your problem here, because
Ada.Strings.Unbounded does not contain any Put subprogram that might
collide with those in Ada.Text_IO or Ada.Integer_Text_IO.

Instead, I think that your problem stems from the the use clause
making the following operators directly visible (from ARM A.4.5):

 15.      function "&" (Left, Right : in Unbounded_String)
             return Unbounded_String;

 16.      function "&" (Left : in Unbounded_String; Right : in String)
             return Unbounded_String;

 17.      function "&" (Left : in String; Right : in Unbounded_String)
             return Unbounded_String;

 18.      function "&" (Left : in Unbounded_String; Right : in Character)
             return Unbounded_String;

 19.      function "&" (Left : in Character; Right : in Unbounded_String)
             return Unbounded_String;

so now you're constructing unbounded strings by concatenating strings
and unbounded strings, and passing them to Ada.Text_IO.Put, which is
of course illegal.

You should convert the unbounded strings to regular strings before
passing them to Ada.Text_IO.Put, like so:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;
procedure Foo is
   S : Unbounded_String := "Hello" & To_Unbounded_String (", world!");
   -- calls Ada.Strings.Unbounded."&" which is directly visible
begin
   Ada.Text_IO.Put (To_String (S)); -- convert and print
end Foo;

HTH

-- 
Ludovic Brenta.



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

* Re: STRING length
  2006-11-15  1:09     ` markww
@ 2006-11-15  1:21       ` Ludovic Brenta
  2006-11-15  1:29       ` Jeffrey R. Carter
  1 sibling, 0 replies; 14+ messages in thread
From: Ludovic Brenta @ 2006-11-15  1:21 UTC (permalink / raw)


"markww" <markww@gmail.com> writes:

> Actually please disregard the previous post, I found I needed to with
> and use:
>
>     Ada.Text_IO.Unbounded_IO
>
> So.. finally one compilation error remains, the actual call to my
> function:
>
>     begin
>
>         Add_Record("Mark", "555-555-5555", "123 main street");
>
>     end LinkList;
>
> the error is:
>
>     expected private type "Ada.Strings.Unbounded.Unbounded_String"
>
> what does that mean? The problem is with calling the procedure, if I
> comment the call out compilation is successful. The procedure looks
> like:

"Mark" is a regular, not unbounded string.  Try this:

begin
   Add_Record (To_Unbounded_String ("Mark"),
               To_Unbounded_String ("555-555-5555"),
               To_Unbounded_String ("123 main street"));
end;

You may also like:

declare
   function "+" (R : in String) return Unbounded_String
      renames To_Unbounded_String;
begin
   Add_Record (+"Mark", +"555-555-5555", +"123 main street");
end;

-- 
Ludovic Brenta.



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

* Re: STRING length
  2006-11-14 22:51 STRING length markww
  2006-11-14 22:24 ` Georg Bauhaus
@ 2006-11-15  1:27 ` Jeffrey R. Carter
  2006-11-15 16:28 ` Martin Krischik
  2006-11-16  8:25 ` Jerry
  3 siblings, 0 replies; 14+ messages in thread
From: Jeffrey R. Carter @ 2006-11-15  1:27 UTC (permalink / raw)


markww wrote:
> 
>     type MY_RECORD is
>       record
>          m_strName     : STRING(1..5);
>          m_strPhone    : STRING(1..5);
>          m_strAddress  : STRING(1..5);
>       end record;

String is a 1D array type, and all 1D array objects in Ada have a fixed 
length.

This is pretty basic Ada. I'd suggest you work through one of the 
tutorials at adapower.com or adaworld.com, and perhaps look at some of 
the on-line textbooks before proceeding.

-- 
Jeff Carter
"Help! Help! I'm being repressed!"
Monty Python & the Holy Grail
67



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

* Re: STRING length
  2006-11-15  1:09     ` markww
  2006-11-15  1:21       ` Ludovic Brenta
@ 2006-11-15  1:29       ` Jeffrey R. Carter
  2006-11-15  2:13         ` markww
  1 sibling, 1 reply; 14+ messages in thread
From: Jeffrey R. Carter @ 2006-11-15  1:29 UTC (permalink / raw)


markww wrote:
>         Add_Record("Mark", "555-555-5555", "123 main street");
> 
>     expected private type "Ada.Strings.Unbounded.Unbounded_String"

As the message says, Unbounded_String is a private type. String literals 
are only defined for string types. You need to supply values of type 
Unbounded_String here. You might want to spend a moment reviewing the 
operations in Ada.Strings.Unbounded to see how to do that.

-- 
Jeff Carter
"Help! Help! I'm being repressed!"
Monty Python & the Holy Grail
67



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

* Re: STRING length
  2006-11-15  1:29       ` Jeffrey R. Carter
@ 2006-11-15  2:13         ` markww
  0 siblings, 0 replies; 14+ messages in thread
From: markww @ 2006-11-15  2:13 UTC (permalink / raw)


Yay fantastic To_Unbounded_String() cleared it up. I have been going
through the tutorials at:

    http://www.infres.enst.fr/~pautet/Ada95/a95list.htm

but they only use fixed length strings there. Thanks for the link with
the other tutorials, I'll check it out.

Thanks for everyone's help as always,

Mark



On Nov 14, 8:29 pm, "Jeffrey R. Carter"
<spam.not.jrcar...@acm.not.spam.org> wrote:
> markww wrote:
> >         Add_Record("Mark", "555-555-5555", "123 main street");
>
> >     expected private type "Ada.Strings.Unbounded.Unbounded_String"As the message says, Unbounded_String is a private type. String literals
> are only defined for string types. You need to supply values of type
> Unbounded_String here. You might want to spend a moment reviewing the
> operations in Ada.Strings.Unbounded to see how to do that.
>
> --
> Jeff Carter
> "Help! Help! I'm being repressed!"
> Monty Python & the Holy Grail
> 67




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

* Re: STRING length
  2006-11-14 22:51 STRING length markww
  2006-11-14 22:24 ` Georg Bauhaus
  2006-11-15  1:27 ` Jeffrey R. Carter
@ 2006-11-15 16:28 ` Martin Krischik
  2006-11-16  8:25 ` Jerry
  3 siblings, 0 replies; 14+ messages in thread
From: Martin Krischik @ 2006-11-15 16:28 UTC (permalink / raw)


markww wrote:

> Hi,
> 
> How does one use a variable length string in ada? I created a record
> like:
> 
>     type MY_RECORD is
>       record
>          m_strName     : STRING(1..5);
>          m_strPhone    : STRING(1..5);
>          m_strAddress  : STRING(1..5);
>       end record;
> 
> However when trying to assign the members of the record, at runtime I
> may get an exception if the assigned string is not exactly 5
> characters. Is there a different STRING type which doesn't care how
> long the assigned string is (as long as it's shorter than the max) or
> which just grows to meet the assigned string length?

http://en.wikibooks.org/wiki/Ada_Programming/Strings

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



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

* Re: STRING length
  2006-11-14 22:51 STRING length markww
                   ` (2 preceding siblings ...)
  2006-11-15 16:28 ` Martin Krischik
@ 2006-11-16  8:25 ` Jerry
  3 siblings, 0 replies; 14+ messages in thread
From: Jerry @ 2006-11-16  8:25 UTC (permalink / raw)



markww wrote:
> Hi,
>
> How does one use a variable length string in ada? I created a record
> like:
>
You might like the ability to define Borland-style strings. You can
download a package containing such from here:
http://homepage.sunrise.ch/mysunrise/gdm/gsoft.htm

>From here http://homepage.sunrise.ch/mysunrise/gdm/pascada.htm it seems
that the gist of it is thus:

  type BorString( maxlength: positive ) is record
    length: Natural:= 0;
    s: String( 1..maxlength );
  end record;

Jerry




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

end of thread, other threads:[~2006-11-16  8:25 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-14 22:51 STRING length markww
2006-11-14 22:24 ` Georg Bauhaus
2006-11-15  0:44   ` markww
2006-11-15  1:09     ` markww
2006-11-15  1:21       ` Ludovic Brenta
2006-11-15  1:29       ` Jeffrey R. Carter
2006-11-15  2:13         ` markww
2006-11-15  1:17     ` Ludovic Brenta
2006-11-15  1:27 ` Jeffrey R. Carter
2006-11-15 16:28 ` Martin Krischik
2006-11-16  8:25 ` Jerry
  -- strict thread matches above, loose matches on Subject: below --
1998-10-01  0:00 String Length mincus
1998-10-02  0:00 ` Pascal Obry
1998-10-02  0:00   ` Ehud Lamm

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