comp.lang.ada
 help / color / mirror / Atom feed
* questions from a newbie
@ 2004-07-15 13:20 zork
  2004-07-15 13:35 ` Marius Amado Alves
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: zork @ 2004-07-15 13:20 UTC (permalink / raw)


Hi, I just started a course in ada. I just have 2 questions at present.

-------------
q1) can:

c : character;

if c in 'A'..'Z' or c in 'a'..'z' or c in '0'..'9' then
   ....
end if;

be written as something like:

c : character;

if c in ('A'..'Z', 'a'..'z', '0'..'9') then
   ....
end if;
-------------

Also, I know you can do the following:

type new_type is array(1..20) of string(1..50);
words : new_type;
index : integer := 20;
words (15) (index ..index) := "K";

however I find that I cannot instead say:

words(15)(index):="K";

why is this so? I get a "Type mismatch in assignment statement, continuing"
error. It does however work when I use words(15)(index):='K'. The rational
behind this is that (index..index) represents a range - hence a string -
whereas (index) represents a single character?

Any insight most helpful.

Cheers,
zork





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

* Re: questions from a newbie
  2004-07-15 13:20 questions from a newbie zork
@ 2004-07-15 13:35 ` Marius Amado Alves
  2004-07-15 13:45 ` Steve
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Marius Amado Alves @ 2004-07-15 13:35 UTC (permalink / raw)
  To: comp.lang.ada

> words(15)(index):="K";
> 
> why is this so? I get a "Type mismatch in assignment statement, continuing"
> error. It does however work when I use words(15)(index):='K'. The rational
> behind this is that (index..index) represents a range - hence a string -
> whereas (index) represents a single character?

Yes. 'K'. Single quotes.




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

* Re: questions from a newbie
  2004-07-15 13:20 questions from a newbie zork
  2004-07-15 13:35 ` Marius Amado Alves
@ 2004-07-15 13:45 ` Steve
  2004-07-15 14:44 ` Georg Bauhaus
  2004-07-15 15:09 ` Jacob Sparre Andersen
  3 siblings, 0 replies; 6+ messages in thread
From: Steve @ 2004-07-15 13:45 UTC (permalink / raw)


"zork" <zork@nospam.com> wrote in message news:40f684a8@dnews.tpgi.com.au...
> Hi, I just started a course in ada. I just have 2 questions at present.
>
> -------------
> q1) can:
>
> c : character;
>
> if c in 'A'..'Z' or c in 'a'..'z' or c in '0'..'9' then
>    ....
> end if;
>
> be written as something like:
>
> c : character;
>
> if c in ('A'..'Z', 'a'..'z', '0'..'9') then
>    ....
> end if;
> -------------

Not directly, but you can make use the of standard Ada libraries:
  Ada.Strings.Maps
  Ada.Strings.Maps.Constants
and do something like:

  if Is_In( c, Alphanumeric_Set ) then
    ...
  end if;

Since you're a newbie, I recommend you peruse annex A of the Ada 95
reference manual; it describes the predefined language enviroment, which is
basically a list of all of the standard libraries.

You can download a copy from:
    http://www.adaic.org/standards/ada95.html

>
> Also, I know you can do the following:
>
> type new_type is array(1..20) of string(1..50);
> words : new_type;
> index : integer := 20;
> words (15) (index ..index) := "K";
>
> however I find that I cannot instead say:
>
> words(15)(index):="K";
>
> why is this so? I get a "Type mismatch in assignment statement,
continuing"
> error. It does however work when I use words(15)(index):='K'. The rational
> behind this is that (index..index) represents a range - hence a string -
> whereas (index) represents a single character?
>

You answered your own question here.

If you're coming from a different programming language, you may find that
Ada is kind of a "hard ass" about the syntax it will accept.  When you get
over the frustration and find that more programs work correctly after you
get past the compiler, you may never want to go back.

Steve
(The Duck)

> Any insight most helpful.
>
> Cheers,
> zork
>
>





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

* Re: questions from a newbie
  2004-07-15 13:20 questions from a newbie zork
  2004-07-15 13:35 ` Marius Amado Alves
  2004-07-15 13:45 ` Steve
@ 2004-07-15 14:44 ` Georg Bauhaus
  2004-07-15 15:09 ` Jacob Sparre Andersen
  3 siblings, 0 replies; 6+ messages in thread
From: Georg Bauhaus @ 2004-07-15 14:44 UTC (permalink / raw)


zork <zork@nospam.com> wrote:
: 
: if c in 'A'..'Z' or c in 'a'..'z' or c in '0'..'9' then
: 
: 
: if c in ('A'..'Z', 'a'..'z', '0'..'9') then

This is the purpose of Ada.Strings.Maps etc.
In Ada.Strings.Maps.Constants you will find predefined
Character_Set constant that match yours.

: Also, I know you can do the following:
: 
: type new_type is array(1..20) of string(1..50);
: words : new_type;
: index : integer := 20;
: words (15) (index ..index) := "K";
: 
: however I find that I cannot instead say:
: 
: words(15)(index):="K";
: 
: why is this so?

...(index) denotes a Character, one component of an array,
...(index .. index) denotes an array slice.
The index values can in general be results of computations at
runtime, i.e., ...(n .. m).  What kind of thing other than
an array slice should (n .. m) denote? OTOH, ...(n) where n
is an index value cannot but denote one array component.

: I get a "Type mismatch in assignment statement, continuing"
: error. It does however work when I use words(15)(index):='K'. The rational
: behind this is that (index..index) represents a range - hence a string -
: whereas (index) represents a single character?



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

* Re: questions from a newbie
  2004-07-15 13:20 questions from a newbie zork
                   ` (2 preceding siblings ...)
  2004-07-15 14:44 ` Georg Bauhaus
@ 2004-07-15 15:09 ` Jacob Sparre Andersen
  2004-07-15 15:52   ` zork
  3 siblings, 1 reply; 6+ messages in thread
From: Jacob Sparre Andersen @ 2004-07-15 15:09 UTC (permalink / raw)


Zork wrote:

> q1) can:
> 
> c : character;
> 
> if c in 'A'..'Z' or c in 'a'..'z' or c in '0'..'9' then
>    ....
> end if;
> 
> be written as something like:
> 
> c : character;
> 
> if c in ('A'..'Z', 'a'..'z', '0'..'9') then
>    ....
> end if;

Sort of:

   case C in
      when 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' =>
         ....
      when others =>
         null;
   end case;

Greetings,

Jacob
-- 
"Banning open source would have immediate, broad, and
 strongly negative impacts on the ability of many sensitive
 and security-focused DOD groups to protect themselves
 against cyberattacks"                        -- Mitre Corp.



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

* Re: questions from a newbie
  2004-07-15 15:09 ` Jacob Sparre Andersen
@ 2004-07-15 15:52   ` zork
  0 siblings, 0 replies; 6+ messages in thread
From: zork @ 2004-07-15 15:52 UTC (permalink / raw)


Thanks everyone for the insight. Much appreciated. Im liking ada. Previously
programmed in c++ and java.

cheers,
zork


"Jacob Sparre Andersen" <sparre@nbi.dk> wrote in message
news:pl3c3tcnny.fsf@sparre.crs4.it...
> Zork wrote:
>
> > q1) can:
> >
> > c : character;
> >
> > if c in 'A'..'Z' or c in 'a'..'z' or c in '0'..'9' then
> >    ....
> > end if;
> >
> > be written as something like:
> >
> > c : character;
> >
> > if c in ('A'..'Z', 'a'..'z', '0'..'9') then
> >    ....
> > end if;
>
> Sort of:
>
>    case C in
>       when 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' =>
>          ....
>       when others =>
>          null;
>    end case;
>
> Greetings,
>
> Jacob
> -- 
> "Banning open source would have immediate, broad, and
>  strongly negative impacts on the ability of many sensitive
>  and security-focused DOD groups to protect themselves
>  against cyberattacks"                        -- Mitre Corp.





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

end of thread, other threads:[~2004-07-15 15:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-15 13:20 questions from a newbie zork
2004-07-15 13:35 ` Marius Amado Alves
2004-07-15 13:45 ` Steve
2004-07-15 14:44 ` Georg Bauhaus
2004-07-15 15:09 ` Jacob Sparre Andersen
2004-07-15 15:52   ` zork

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