comp.lang.ada
 help / color / mirror / Atom feed
* trimming strings
@ 2014-08-02 13:10 agent
  2014-08-02 14:21 ` Pascal Obry
  2014-08-02 17:22 ` mockturtle
  0 siblings, 2 replies; 15+ messages in thread
From: agent @ 2014-08-02 13:10 UTC (permalink / raw)


I am having a very difficult time understanding something.  I am
trying to do this using gnat on Ubuntu 14.04 system:
 
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;

subtype string255fixedtype is string (1.255);
inbuf : string255fixedtype;

BEGIN
  inbuf := Get_Line;
  inbuf := trim(inbuf,both);  
--    this does not work, error is both is not visible

No combination of ada.strings.fixed.both, or ada.strings.both got it
to be visible.

However, I was able to get left and right to work, by using
ada.strings.left and ada.strings.right.

Trim_end is an enumeration type that says (left,right,both) in the
books I've looked at.

What am I missing?

Thanks

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

* Re: trimming strings
  2014-08-02 13:10 trimming strings agent
@ 2014-08-02 14:21 ` Pascal Obry
  2014-08-02 15:27   ` G.B.
  2014-08-02 17:22 ` mockturtle
  1 sibling, 1 reply; 15+ messages in thread
From: Pascal Obry @ 2014-08-02 14:21 UTC (permalink / raw)


Le samedi 02 août 2014 à 09:10 -0400, agent@drrob1.com a écrit : 
> I am having a very difficult time understanding something.  I am
> trying to do this using gnat on Ubuntu 14.04 system:
>  
> with Ada.Strings; use Ada.Strings;
> with Ada.Strings.Fixed; use Ada.Strings.Fixed;
> 
> subtype string255fixedtype is string (1.255);
> inbuf : string255fixedtype;
> 
> BEGIN
>   inbuf := Get_Line;
>   inbuf := trim(inbuf,both);  
> --    this does not work, error is both is not visible

Please post a complete example, there is many errors above, far to many
to even pass the compilation.

-- 
  Pascal Obry /  Magny Les Hameaux (78)

  The best way to travel is by means of imagination

  http://v2p.fr.eu.org
  http://www.obry.net

  gpg --keyserver keys.gnupg.net --recv-key F949BD3B




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

* Re: trimming strings
  2014-08-02 14:21 ` Pascal Obry
@ 2014-08-02 15:27   ` G.B.
  2014-08-02 17:00     ` Adam Beneschan
  0 siblings, 1 reply; 15+ messages in thread
From: G.B. @ 2014-08-02 15:27 UTC (permalink / raw)


>> subtype string255fixedtype is string (1.255);
>> inbuf : string255fixedtype;
>> 
>> BEGIN
>>   inbuf := Get_Line;
>>   inbuf := trim(inbuf,both);  
>> --    this does not work, error is both is not visible
> 
> Please post a complete example, there is many errors above, far to many
> to even pass the compilation.

one of these being likely that an array of 255 characters
is to receive a string object that may not have that many
due to trimming.


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

* Re: trimming strings
  2014-08-02 15:27   ` G.B.
@ 2014-08-02 17:00     ` Adam Beneschan
  0 siblings, 0 replies; 15+ messages in thread
From: Adam Beneschan @ 2014-08-02 17:00 UTC (permalink / raw)


On Saturday, August 2, 2014 8:27:42 AM UTC-7, G. B. wrote:
> >> subtype string255fixedtype is string (1.255);
> >> inbuf : string255fixedtype;

> >> BEGIN
> >>   inbuf := Get_Line;
> >>   inbuf := trim(inbuf,both);  
> >> --    this does not work, error is both is not visible

> > Please post a complete example, there is many errors above, far to many
> > to even pass the compilation.
> 
> 
> one of these being likely that an array of 255 characters
> is to receive a string object that may not have that many
> due to trimming.

That's definitely an error (and assigning a string with a known length to Get_Line won't work either).  But those won't cause errors at compile time.  They will result in exceptions at run time.

                                 -- Adam

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

* Re: trimming strings
  2014-08-02 13:10 trimming strings agent
  2014-08-02 14:21 ` Pascal Obry
@ 2014-08-02 17:22 ` mockturtle
  2014-08-03 21:42   ` agent
  1 sibling, 1 reply; 15+ messages in thread
From: mockturtle @ 2014-08-02 17:22 UTC (permalink / raw)


On Saturday, August 2, 2014 3:10:16 PM UTC+2, ag...@drrob1.com wrote:
> I am having a very difficult time understanding something.  I am
> 
> trying to do this using gnat on Ubuntu 14.04 system:
> 
> with Ada.Strings; use Ada.Strings;
> with Ada.Strings.Fixed; use Ada.Strings.Fixed;
> 
> subtype string255fixedtype is string (1.255);
> inbuf : string255fixedtype;
> 
> BEGIN
> 
>   inbuf := Get_Line;
>   inbuf := trim(inbuf,both);  
> 
> --    this does not work, error is both is not visible
> 
> No combination of ada.strings.fixed.both, or ada.strings.both got it
> to be visible.


As others said, a complete example would help us to help you.  Anyway,  I am going to do  a wild guessing: did you maybe declared another "both" in your code?  I do not have a compiler at hand and I cannot check, but if I remember correctly in this case you get a "non visible" error because the two symbols hide each other.

Riccardo

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

* Re: trimming strings
  2014-08-02 17:22 ` mockturtle
@ 2014-08-03 21:42   ` agent
  2014-08-03 23:14     ` Georg Bauhaus
  2014-08-04  0:17     ` Shark8
  0 siblings, 2 replies; 15+ messages in thread
From: agent @ 2014-08-03 21:42 UTC (permalink / raw)


On Sat, 2 Aug 2014 10:22:26 -0700 (PDT), mockturtle
<framefritti@gmail.com> wrote:

>On Saturday, August 2, 2014 3:10:16 PM UTC+2, ag...@drrob1.com wrote:
>> I am having a very difficult time understanding something.  I am
>> 
>> trying to do this using gnat on Ubuntu 14.04 system:
>> 
>> with Ada.Strings; use Ada.Strings;
>> with Ada.Strings.Fixed; use Ada.Strings.Fixed;
>> 
>> subtype string255fixedtype is string (1.255);
>> inbuf : string255fixedtype;
>> 
>> BEGIN
>> 
>>   inbuf := Get_Line;
>>   inbuf := trim(inbuf,both);  
>> 
>> --    this does not work, error is both is not visible
>> 
>> No combination of ada.strings.fixed.both, or ada.strings.both got it
>> to be visible.
>
>
>As others said, a complete example would help us to help you.  Anyway,  I am going to do  a wild guessing: did you maybe declared another "both" in your code?  I do not have a compiler at hand and I cannot check, but if I remember correctly in this case you get a "non visible" error because the two symbols hide each other.
>
>Riccardo


I am, after all, a newbie in Ada.  I was wondering what the
non-visible error meant, as I was getting that also.  Now I know it
means that identifiers are clashing in different packages.

I'm guessing that it is the use statements that make the symbols
clash?

I also don't have string processing down.  


> one of these being likely that an array of 255 characters
> is to receive a string object that may not have that many
> due to trimming.

> That's definitely an error (and assigning a string with a known length to Get_Line won't work either).  
> But those won't cause errors at compile time.  They will result in exceptions at run time.
>                                 -- Adam


with Ada.Text_IO; use Ada.Text_IO;
with Ada.Characters; use Ada.Characters;
with Ada.Characters.Conversions;  use Ada.Characters.Conversions;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
---------------------------

Procedure trimtest is
  Subtype String255FixedType is String(1..255); 

  str    : String255Fixedtype;
  STRLEN : Natural;
 
BEGIN
  Put(" Enter line: ");
  Get_Line(Str,StrLen);
  Str := TRIM(Str,Side => Both);
--  Str := Ada.Strings.Fixed.TRIM(str,side => Ada.Strings.both);
  Put_Line(" Line is: " & Str(1..StrLen) & " with length of " &
Natural'image(StrLen) );
End trimtest;

This simple procedure compiles, but does give me an exception at
run-time after I call trim.  I don't understand how to avoid this.  I
am used to more flexible strings that are null terminated.

How do I avoid a constraint_error exception at run-time?

Thanks

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

* Re: trimming strings
  2014-08-03 21:42   ` agent
@ 2014-08-03 23:14     ` Georg Bauhaus
  2014-08-04  0:17     ` Shark8
  1 sibling, 0 replies; 15+ messages in thread
From: Georg Bauhaus @ 2014-08-03 23:14 UTC (permalink / raw)


On 03.08.14 23:42, agent@drrob1.com wrote:

> Procedure trimtest is
>    Subtype String255FixedType is String(1..255);
>
>    str    : String255Fixedtype;
>    STRLEN : Natural;
>
> BEGIN
>    Put(" Enter line: ");
>    Get_Line(Str,StrLen);
>    Str := TRIM(Str,Side => Both);
> --  Str := Ada.Strings.Fixed.TRIM(str,side => Ada.Strings.both);
>    Put_Line(" Line is: " & Str(1..StrLen) & " with length of " &
> Natural'image(StrLen) );
> End trimtest;
>
> This simple procedure compiles, but does give me an exception at
> run-time after I call trim.  I don't understand how to avoid this.  I
> am used to more flexible strings that are null terminated.

These are Ada.Strings.Bounded and Ada.Strings.Unbounded. Or,
if you want the greatest possible flexibility, wrap a pointer
to String in a type derived from Finalization.Controlled. This,
however, will likely turn into reinventing either of the former
language defined packages.

An object of type String255FixedType will always be what it is
by declaration: an array of exactly 255 components, indexed
by values between 1 and 255, inclusively. So, "fixed" is a
well chosen part of the subtype's name.

> How do I avoid a constraint_error exception at run-time?

One more way is to declare the string object where you need it

    Get_Line (Str, StrLen);
    declare
       Trimmed : String := Trim (Str(1 .. StrLen), Side => Both);
    begin
        ... do something with Trimmed
    end;

Or pass the trimmed result to some procedure that works on it:

    procedure Taking_Trimmed (S : String) is
       --  S is of any length, and of any indexing subtype
    begin
       -- note: S'Length, S'First, etc are known here
    end Taking_Trimmed;
    ...
    Get_Line (Str, StrLen);
    Taking_Trimmed (Trim (Str(1 .. StrLen), Side => Both));





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

* Re: trimming strings
  2014-08-03 21:42   ` agent
  2014-08-03 23:14     ` Georg Bauhaus
@ 2014-08-04  0:17     ` Shark8
  2014-08-04 10:06       ` mockturtle
  1 sibling, 1 reply; 15+ messages in thread
From: Shark8 @ 2014-08-04  0:17 UTC (permalink / raw)


On 03-Aug-14 15:42, agent@drrob1.com wrote:
> I also don't have string processing down.

Ah, here's the big secret to Ada string processing: treat them as arrays.

Make liberal use of things like 'First, 'Last, 'Length and don't assume 
your 'Fists = 1. Also, remember the difference between constrained and 
unconstrained --
  D : String;                    -- Unconstrained.
  E : String := "This."          -- Constrained by initialization.
  F : String(1..255);            -- Constrained by subtype.
  G : String := Fn(X);           -- Constrained by initialization.

  -- the last thing to remember is that strings are perfectly sized for 
their content. (This should be done at initialization.) TED explains it 
quite well here: http://stackoverflow.com/a/15104158/608963

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

* Re: trimming strings
  2014-08-04  0:17     ` Shark8
@ 2014-08-04 10:06       ` mockturtle
  2014-08-04 10:49         ` Pascal Obry
  2014-08-04 16:50         ` Shark8
  0 siblings, 2 replies; 15+ messages in thread
From: mockturtle @ 2014-08-04 10:06 UTC (permalink / raw)


On Monday, August 4, 2014 2:17:28 AM UTC+2, Shark8 wrote:
> 
> Make liberal use of things like 'First, 'Last, 'Length and don't assume 
> your 'Fists = 1. 
        ^^^^^
          
          
This is what I would call "aggressive programming" :-) :-) :-)

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

* Re: trimming strings
  2014-08-04 10:06       ` mockturtle
@ 2014-08-04 10:49         ` Pascal Obry
  2014-08-04 11:51           ` Simon Clubley
  2014-08-04 16:50         ` Shark8
  1 sibling, 1 reply; 15+ messages in thread
From: Pascal Obry @ 2014-08-04 10:49 UTC (permalink / raw)


Le lundi 04 août 2014 à 03:06 -0700, mockturtle a écrit : 
> On Monday, August 4, 2014 2:17:28 AM UTC+2, Shark8 wrote:
> > 
> > Make liberal use of things like 'First, 'Last, 'Length and don't assume 
> > your 'Fists = 1. 
>         ^^^^^
>           
>           
> This is what I would call "aggressive programming" :-) :-) :-)

Not really, you don't want to assume a string will start to 1.

   procedure Call (Str : in out String) is
   begin
     Str (1) := 'b'; -- assuming 'First is 1 is wrong
   end Call;

   Whatever : String := "une belle phrase!";

Then the following call will just fail:

   Call (Whatever (4 .. 8));

-- 
  Pascal Obry /  Magny Les Hameaux (78)

  The best way to travel is by means of imagination

  http://v2p.fr.eu.org
  http://www.obry.net

  gpg --keyserver keys.gnupg.net --recv-key F949BD3B




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

* Re: trimming strings
  2014-08-04 10:49         ` Pascal Obry
@ 2014-08-04 11:51           ` Simon Clubley
  2014-08-04 12:11             ` Pascal Obry
  0 siblings, 1 reply; 15+ messages in thread
From: Simon Clubley @ 2014-08-04 11:51 UTC (permalink / raw)


On 2014-08-04, Pascal Obry <pascal@obry.net> wrote:
> Le lundi 04 août 2014 à 03:06 -0700, mockturtle a écrit : 
>> On Monday, August 4, 2014 2:17:28 AM UTC+2, Shark8 wrote:
>> > 
>> > Make liberal use of things like 'First, 'Last, 'Length and don't assume 
>> > your 'Fists = 1. 
>>         ^^^^^
>>           
>>           
>> This is what I would call "aggressive programming" :-) :-) :-)
>
> Not really, you don't want to assume a string will start to 1.
>

mockturtle was making a comment on the typo of "'Fists" instead of
"'First". :-)

Simon.

-- 
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world

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

* Re: trimming strings
  2014-08-04 11:51           ` Simon Clubley
@ 2014-08-04 12:11             ` Pascal Obry
  0 siblings, 0 replies; 15+ messages in thread
From: Pascal Obry @ 2014-08-04 12:11 UTC (permalink / raw)


Le lundi 04 août 2014 à 11:51 +0000, Simon Clubley a écrit : 
> mockturtle was making a comment on the typo of "'Fists" instead of
> "'First". :-)

Missed the joke, sorry :)

-- 
  Pascal Obry /  Magny Les Hameaux (78)

  The best way to travel is by means of imagination

  http://v2p.fr.eu.org
  http://www.obry.net

  gpg --keyserver keys.gnupg.net --recv-key F949BD3B




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

* Re: trimming strings
  2014-08-04 10:06       ` mockturtle
  2014-08-04 10:49         ` Pascal Obry
@ 2014-08-04 16:50         ` Shark8
  2014-08-04 23:28           ` Dennis Lee Bieber
  1 sibling, 1 reply; 15+ messages in thread
From: Shark8 @ 2014-08-04 16:50 UTC (permalink / raw)


On 04-Aug-14 04:06, mockturtle wrote:
>> Make liberal use of things like 'First, 'Last, 'Length and don't assume
>> >your 'Fists = 1.
>          ^^^^^
>
>
> This is what I would call "aggressive programming"

Sometimes programming *JUST MAKES ME ANGRY!*
;)


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

* Re: trimming strings
  2014-08-04 16:50         ` Shark8
@ 2014-08-04 23:28           ` Dennis Lee Bieber
  2014-08-05 21:05             ` agent
  0 siblings, 1 reply; 15+ messages in thread
From: Dennis Lee Bieber @ 2014-08-04 23:28 UTC (permalink / raw)


On Mon, 04 Aug 2014 10:50:16 -0600, Shark8 <OneWingedShark@gmail.com>
declaimed the following:

>On 04-Aug-14 04:06, mockturtle wrote:
>>> Make liberal use of things like 'First, 'Last, 'Length and don't assume
>>> >your 'Fists = 1.
>>          ^^^^^
>>
>>
>> This is what I would call "aggressive programming"
>
>Sometimes programming *JUST MAKES ME ANGRY!*
>;)

	Now repeat that in the voice of Marvin the Martian <G>

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/


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

* Re: trimming strings
  2014-08-04 23:28           ` Dennis Lee Bieber
@ 2014-08-05 21:05             ` agent
  0 siblings, 0 replies; 15+ messages in thread
From: agent @ 2014-08-05 21:05 UTC (permalink / raw)


On Mon, 04 Aug 2014 19:28:57 -0400, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:

>On Mon, 04 Aug 2014 10:50:16 -0600, Shark8 <OneWingedShark@gmail.com>
>declaimed the following:
>
>>On 04-Aug-14 04:06, mockturtle wrote:
>>>> Make liberal use of things like 'First, 'Last, 'Length and don't assume
>>>> >your 'Fists = 1.
>>>          ^^^^^
>>>
>>>
>>> This is what I would call "aggressive programming"
>>
>>Sometimes programming *JUST MAKES ME ANGRY!*
>>;)
>
>	Now repeat that in the voice of Marvin the Martian <G>

LOL.

And thanks for the help.


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

end of thread, other threads:[~2014-08-05 21:05 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-02 13:10 trimming strings agent
2014-08-02 14:21 ` Pascal Obry
2014-08-02 15:27   ` G.B.
2014-08-02 17:00     ` Adam Beneschan
2014-08-02 17:22 ` mockturtle
2014-08-03 21:42   ` agent
2014-08-03 23:14     ` Georg Bauhaus
2014-08-04  0:17     ` Shark8
2014-08-04 10:06       ` mockturtle
2014-08-04 10:49         ` Pascal Obry
2014-08-04 11:51           ` Simon Clubley
2014-08-04 12:11             ` Pascal Obry
2014-08-04 16:50         ` Shark8
2014-08-04 23:28           ` Dennis Lee Bieber
2014-08-05 21:05             ` agent

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