comp.lang.ada
 help / color / mirror / Atom feed
* Variable length string
@ 2001-08-13  8:19 Shitij
  2001-08-13 10:20 ` Petter Fryklund
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Shitij @ 2001-08-13  8:19 UTC (permalink / raw)


Hi,
Iam new to ADA.I just wanted to know how do you define a variable length
string.I have declared a string array of length say a size of 20.But if I
use get and pass this string as an arguement,then unless and until I fill
this with exactly 20 characters my program is not working.How to overcome
this?
Iam using the below structure

type list;
 type pList is access list;
 type list is
record
   data:integer;
   str:string(1 .. 20);
   next:pList:=NULL;
  end record;





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

* Re: Variable length string
  2001-08-13  8:19 Variable length string Shitij
@ 2001-08-13 10:20 ` Petter Fryklund
  2001-08-13 10:40 ` Des Walker
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Petter Fryklund @ 2001-08-13 10:20 UTC (permalink / raw)


You might find Ada.Strings.FIxed.Move useful!

Shitij wrote in message <9l82gv$j83$1@news.mch.sbs.de>...
>Hi,
>Iam new to ADA.I just wanted to know how do you define a variable length
>string.I have declared a string array of length say a size of 20.But if I
>use get and pass this string as an arguement,then unless and until I fill
>this with exactly 20 characters my program is not working.How to overcome
>this?
>Iam using the below structure
>
>type list;
> type pList is access list;
> type list is
>record
>   data:integer;
>   str:string(1 .. 20);
>   next:pList:=NULL;
>  end record;
>
>





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

* Re: Variable length string
  2001-08-13  8:19 Variable length string Shitij
  2001-08-13 10:20 ` Petter Fryklund
@ 2001-08-13 10:40 ` Des Walker
  2001-08-13 14:31   ` Des Walker
  2001-08-13 13:05 ` Larry Kilgallen
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Des Walker @ 2001-08-13 10:40 UTC (permalink / raw)


Shitij wrote:
> 
> Hi,
> Iam new to ADA.I just wanted to know how do you define a variable length
> string.I have declared a string array of length say a size of 20.But if I
> use get and pass this string as an arguement,then unless and until I fill
> this with exactly 20 characters my program is not working.How to overcome
> this?
> Iam using the below structure
> 
> type list;
>  type pList is access list;
>  type list is
> record
>    data:integer;
>    str:string(1 .. 20);
>    next:pList:=NULL;
>   end record;

Hi,

I think the Text_Io Get procedure attempts to get as many characters as
the length of the supplied string and this can include multiple line
feed characters.

If you expect to input a single line of text you could use the Get_Line
procedure. This returns the index in the string of the last character
read, and you could store this information in your record so that you
could always access the appropriate slice of your string.

Alternatively, you could try using unbounded strings, they're a little
weightier on performance compared to standard strings, but keep track of
the length for you. An example of their use can be found through Section
8 of the Lovelace Tutorial.

http://www.adahome.com/Tutorials/Lovelace/lovelace.html

select the link of 'The master outline of all lessons'

	Regards
	Des Walker



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

* Re: Variable length string
  2001-08-13  8:19 Variable length string Shitij
  2001-08-13 10:20 ` Petter Fryklund
  2001-08-13 10:40 ` Des Walker
@ 2001-08-13 13:05 ` Larry Kilgallen
  2001-08-13 20:04   ` James Rogers
  2001-08-14  3:18 ` DuckE
  2001-08-15 21:27 ` file13
  4 siblings, 1 reply; 9+ messages in thread
From: Larry Kilgallen @ 2001-08-13 13:05 UTC (permalink / raw)


In article <9l82gv$j83$1@news.mch.sbs.de>, "Shitij" <shitij.sah@sisl.co.in> writes:
> Hi,
> Iam new to ADA.I just wanted to know how do you define a variable length
> string.I have declared a string array of length say a size of 20.But if I
> use get and pass this string as an arguement,then unless and until I fill
> this with exactly 20 characters my program is not working.How to overcome
> this?
> Iam using the below structure
> 
> type list;
>  type pList is access list;
>  type list is
> record
>    data:integer;
>    str:string(1 .. 20);
>    next:pList:=NULL;
>   end record;

My suggestion would be to consider the use of a discriminated record
(that term should be in the index of your textbook).  That way you
can make each string be exactly the right length.  You should have
a separate large string for a buffer to do the initial Get call for
each string.  Once you have received it, you know the proper length.



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

* Re: Variable length string
  2001-08-13 10:40 ` Des Walker
@ 2001-08-13 14:31   ` Des Walker
  2001-08-13 15:07     ` Larry Kilgallen
  0 siblings, 1 reply; 9+ messages in thread
From: Des Walker @ 2001-08-13 14:31 UTC (permalink / raw)


Des Walker wrote:
> 
> Hi,
> 
> I think the Text_Io Get procedure attempts to get as many characters as
> the length of the supplied string and this can include multiple line
> feed characters.
> 

No, I was wrong here. Rereading the ARM suggests the Get procedure for a
string
"Determines the length of the given string and attempts that number of
Get operations for successive characters of the string (in particular,
no operation is performed if the string is null)."

and the Get procedure for a Character
"After skipping any line terminators and any page terminators, reads the
next character from the specified input file and returns the value of
this character in the out parameter Item."

	Regards
	Des Walker



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

* Re: Variable length string
  2001-08-13 14:31   ` Des Walker
@ 2001-08-13 15:07     ` Larry Kilgallen
  0 siblings, 0 replies; 9+ messages in thread
From: Larry Kilgallen @ 2001-08-13 15:07 UTC (permalink / raw)


In article <3B77E4A4.50FFA02B@amsjv.com>, Des Walker <des.walker@amsjv.com> writes:
> Des Walker wrote:
>> 
>> Hi,
>> 
>> I think the Text_Io Get procedure attempts to get as many characters as
>> the length of the supplied string and this can include multiple line
>> feed characters.
>> 
> 
> No, I was wrong here. Rereading the ARM suggests the Get procedure for a
> string
> "Determines the length of the given string and attempts that number of
> Get operations for successive characters of the string (in particular,
> no operation is performed if the string is null)."
> 
> and the Get procedure for a Character
> "After skipping any line terminators and any page terminators, reads the
> next character from the specified input file and returns the value of
> this character in the out parameter Item."

But one should not infer any lack of efficiency in the implementation
due to that description.  Anybody skilled enough to write an Ada compiler
knows how to make Get for a line operate without actual subprogram calls
for each character.  So long as the result is the same from the caller
point of view, semantic equivalence is enough.



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

* Re: Variable length string
  2001-08-13 13:05 ` Larry Kilgallen
@ 2001-08-13 20:04   ` James Rogers
  0 siblings, 0 replies; 9+ messages in thread
From: James Rogers @ 2001-08-13 20:04 UTC (permalink / raw)


Larry Kilgallen wrote:
> 
> In article <9l82gv$j83$1@news.mch.sbs.de>, "Shitij" <shitij.sah@sisl.co.in> writes:
> > Hi,
> > Iam new to ADA.I just wanted to know how do you define a variable length
> > string.I have declared a string array of length say a size of 20.But if I
> > use get and pass this string as an arguement,then unless and until I fill
> > this with exactly 20 characters my program is not working.How to overcome
> > this?
 (code snipped)
> My suggestion would be to consider the use of a discriminated record
> (that term should be in the index of your textbook).  That way you
> can make each string be exactly the right length.  You should have
> a separate large string for a buffer to do the initial Get call for
> each string.  Once you have received it, you know the proper length.

Another possibility is to use the Ada.Strings.Bounded package to create
a string type with a maximum size.

Jim Rogers
Colorado Springs, Colorado USA



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

* Re: Variable length string
  2001-08-13  8:19 Variable length string Shitij
                   ` (2 preceding siblings ...)
  2001-08-13 13:05 ` Larry Kilgallen
@ 2001-08-14  3:18 ` DuckE
  2001-08-15 21:27 ` file13
  4 siblings, 0 replies; 9+ messages in thread
From: DuckE @ 2001-08-14  3:18 UTC (permalink / raw)


If you're new to Ada, I suggest you browse through the facilities built into
the standard Ada libraries.

http://www.ada-auth.org/~acats/arm-html/RM-A.html

For the task at hand, I suggest you have a look at:
  Ada.Strings.Unbounded
 - or -
  Ada.Strings.Bounded


SteveD

"Shitij" <shitij.sah@sisl.co.in> wrote in message
news:9l82gv$j83$1@news.mch.sbs.de...
> Hi,
> Iam new to ADA.I just wanted to know how do you define a variable length
> string.I have declared a string array of length say a size of 20.But if I
> use get and pass this string as an arguement,then unless and until I fill
> this with exactly 20 characters my program is not working.How to overcome
> this?
> Iam using the below structure
>
> type list;
>  type pList is access list;
>  type list is
> record
>    data:integer;
>    str:string(1 .. 20);
>    next:pList:=NULL;
>   end record;
>
>





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

* Re: Variable length string
  2001-08-13  8:19 Variable length string Shitij
                   ` (3 preceding siblings ...)
  2001-08-14  3:18 ` DuckE
@ 2001-08-15 21:27 ` file13
  4 siblings, 0 replies; 9+ messages in thread
From: file13 @ 2001-08-15 21:27 UTC (permalink / raw)


what you need is Ada.Strings.Fixed--as someone mentioned above....

Example:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;

procedure Tester is

    type List;
    type PList is access List;
    type List is
       record
          Data : Integer;
          Str  : String (1 .. 20);
          Next : PList := null;
       end record;

    List1 : List;

begin

    Move ("Howdy", List1.Str);  --  Part of Ada.Strings.Fixed
    Put_Line ("List1.Str: " & List1.Str);

end Tester;


Unbounded and Bounded strings have their own ways to do the same thing.

For more info check out

http://www.adahome.com/rm95/rm9x-A-04-03.html

--
file13
http://haxor.redcommandos.com/~file13/
--
"...war is about killing people and destroying things...that requirement 
is...best met through the use of Ada."
Lt. Col. John A. Hamilton Jr. U.S. Military Academy
http://www.stsc.hill.af.mil/crosstalk/1997/dec/languages.asp


Shitij wrote:
> Hi,
> Iam new to ADA.I just wanted to know how do you define a variable length
> string.I have declared a string array of length say a size of 20.But if I
> use get and pass this string as an arguement,then unless and until I fill
> this with exactly 20 characters my program is not working.How to overcome
> this?
> Iam using the below structure
> 
> type list;
>  type pList is access list;
>  type list is
> record
>    data:integer;
>    str:string(1 .. 20);
>    next:pList:=NULL;
>   end record;




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

end of thread, other threads:[~2001-08-15 21:27 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-13  8:19 Variable length string Shitij
2001-08-13 10:20 ` Petter Fryklund
2001-08-13 10:40 ` Des Walker
2001-08-13 14:31   ` Des Walker
2001-08-13 15:07     ` Larry Kilgallen
2001-08-13 13:05 ` Larry Kilgallen
2001-08-13 20:04   ` James Rogers
2001-08-14  3:18 ` DuckE
2001-08-15 21:27 ` file13

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