comp.lang.ada
 help / color / mirror / Atom feed
* Searching for an object
@ 2000-08-18  0:00 Matthias Teege
  2000-08-21  0:00 ` Gerald Kasner
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Matthias Teege @ 2000-08-18  0:00 UTC (permalink / raw)



Moin,

I've defined the following type: 

type customer is record
   aname  : String( 1..25);
   bname  : String( 1..25);
end record;

Then I create some objects of this type and save all of
them in an table.

Now I want to find a special object with a find procedure
like this (not authentic Ada Code :-)):

procedure find ( Field : in String;
                 What  : in String;
                 Result: out String) is

while end_of_table is false loop
  read(object);
  if object.Field = What then -- field should be 'aname'
                              -- or 'bname'
     Result := object.Field;
     exit; -- We have found it
  end if
end loop;
end find;

I want to search in all Record "Fields" depend on the
procedure parameter.

How is the Ada way?

Many thanks
Matthias

-- 
Matthias Teege -- matthias@mteege.de -- http://emugs.de
make world not war
PGP-Key auf Anfrage





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

* Re: Searching for an object
  2000-08-18  0:00 Searching for an object Matthias Teege
@ 2000-08-21  0:00 ` Gerald Kasner
  2000-08-21  0:00   ` Matthias Teege
  2000-08-21  0:00 ` Ted Dennison
  2000-08-31 21:55 ` John McCabe
  2 siblings, 1 reply; 10+ messages in thread
From: Gerald Kasner @ 2000-08-21  0:00 UTC (permalink / raw)


Matthias Teege schrieb:
> 
> Moin,
> 
> I've defined the following type:
> 
> type customer is record
>    aname  : String( 1..25);
>    bname  : String( 1..25);
> end record;
> 
> Then I create some objects of this type and save all of
> them in an table.
> 
> Now I want to find a special object with a find procedure
> like this (not authentic Ada Code :-)):
> 
> procedure find ( Field : in String;
>                  What  : in String;
>                  Result: out String) is
> 
> while end_of_table is false loop
>   read(object);
>   if object.Field = What then -- field should be 'aname'
>                               -- or 'bname'
>      Result := object.Field;
>      exit; -- We have found it
>   end if
> end loop;
> end find;
> 
> I want to search in all Record "Fields" depend on the
> procedure parameter.
> 
> How is the Ada way?
> 
> Many thanks
> Matthias
> 
> --
> Matthias Teege -- matthias@mteege.de -- http://emugs.de
> make world not war
> PGP-Key auf Anfrage

My respose is no direct answer to your question, but I would 
be more careful about the find procedure in general. If you have 
a long table of customers you'll have to wait very long times. 
Consult a textbook on data structures how to do this much faster.

(In your case a tree may be very useful)

A second remark: Ada is designed to reuse code. Why not using 
already existing well tested libraries ? Abstract data types 
seem to be appropriate. 

-Gerald




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

* Re: Searching for an object
  2000-08-21  0:00 ` Gerald Kasner
@ 2000-08-21  0:00   ` Matthias Teege
  0 siblings, 0 replies; 10+ messages in thread
From: Matthias Teege @ 2000-08-21  0:00 UTC (permalink / raw)


Gerald Kasner <Gerald.Kasner@Physik.Uni-Magdeburg.de> writes:

[...]

> My respose is no direct answer to your question, but I would 
> be more careful about the find procedure in general. If you have 
> a long table of customers you'll have to wait very long times. 
> Consult a textbook on data structures how to do this much faster.

Many thanks for your answer. The code from my example is
only for my Ada learning purposes and not for a real life
application. I'm aware of the problems. ;-)

> (In your case a tree may be very useful)

Maybe, I'll refresh my knowledge of data structures.

> A second remark: Ada is designed to reuse code. Why not using 
> already existing well tested libraries ? Abstract data types 
> seem to be appropriate. 

Yes of course. But I reinvent the wheel only to increase
my Ada experiences. So I'm ever very interested on the Ada "way".

Nevertheless many thanks for your answer. I thought my
newsreader was broken. :-)

Bis dann
Matthias

-- 
Matthias Teege -- matthias@mteege.de -- http://emugs.de
make world not war
PGP-Key auf Anfrage




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

* Re: Searching for an object
  2000-08-18  0:00 Searching for an object Matthias Teege
  2000-08-21  0:00 ` Gerald Kasner
@ 2000-08-21  0:00 ` Ted Dennison
  2000-08-21  0:00   ` Matthias Teege
  2000-08-31 21:55 ` John McCabe
  2 siblings, 1 reply; 10+ messages in thread
From: Ted Dennison @ 2000-08-21  0:00 UTC (permalink / raw)


In article <87og2q9yyp.fsf@moon.mteege.de>,
  Matthias Teege <matthias@mteege.de> wrote:
> I've defined the following type:
>
> type customer is record
>    aname  : String( 1..25);
>    bname  : String( 1..25);
> end record;

This looks shaky right from the start. How do you know how many
characters in these fields are valid? Ada string handling doesn't rely
on special "terminator" characters at the end of the strings. You either
dynamicly size the string to hold the exact number of characters you
will need for the data, you keep around a "length" variable with the
string, or you use one of the types in the Ada.Strings.* packages.


> Now I want to find a special object with a find procedure
> like this (not authentic Ada Code :-)):
>
> procedure find ( Field : in String;
>                  What  : in String;
>                  Result: out String) is
>
> while end_of_table is false loop
>   read(object);
>   if object.Field = What then -- field should be 'aname'
>                               -- or 'bname'
>      Result := object.Field;
>      exit; -- We have found it
>   end if
> end loop;
> end find;
>
> I want to search in all Record "Fields" depend on the
> procedure parameter.

Is object supposed to be some global variable? Otherwise, this looks
like you are familiar with "object" notation in some other "OO"
languages, and are looking for the same notation in Ada. Eg: you want
to call Object.Find (...) rather than Find(Object, ...) Is that your
question?

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Searching for an object
  2000-08-21  0:00 ` Ted Dennison
@ 2000-08-21  0:00   ` Matthias Teege
  2000-08-21  0:00     ` Ted Dennison
  2000-08-21  0:00     ` tmoran
  0 siblings, 2 replies; 10+ messages in thread
From: Matthias Teege @ 2000-08-21  0:00 UTC (permalink / raw)


Ted Dennison <dennison@telepath.com> writes:

> In article <87og2q9yyp.fsf@moon.mteege.de>,
>   Matthias Teege <matthias@mteege.de> wrote:
> > I've defined the following type:
> >
> > type customer is record
> >    aname  : String( 1..25);
> >    bname  : String( 1..25);
> > end record;
> 
> This looks shaky right from the start. How do you know how many
> characters in these fields are valid? Ada string handling doesn't rely

Yes it is really bad code but is only for my learning
purposes. It is a quick hack and I'm very impatient. ;-) 

[...]

> Is object supposed to be some global variable? Otherwise, this looks
> like you are familiar with "object" notation in some other "OO"
> languages, and are looking for the same notation in Ada. Eg: you want
> to call Object.Find (...) rather than Find(Object, ...) Is that your
> question?

Yes, I want to call Object.Find(in_field, for_this_string)
but the only way to implement this I see is: 

        if in_field = "field1" and for_this_string = "foo"
        then
           result = "found";
        if in_field = "field2" and for_this_string = "foo"
        then
           result = "found";

and so on. The problem isn't the "object notation" but the
record notation. I can access an record field with
"record.fieldname = ..." and now I want a variable
"fieldname" to spare myself from the long way described
bellow. I'm looking for a short notation like
"record.$fieldname". I hope I can make you understand. :-) 

Many thanks
Matthias

-- 
Matthias Teege -- matthias@mteege.de -- http://emugs.de
make world not war
PGP-Key auf Anfrage




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

* Re: Searching for an object
  2000-08-21  0:00   ` Matthias Teege
@ 2000-08-21  0:00     ` Ted Dennison
  2000-08-21  0:00     ` tmoran
  1 sibling, 0 replies; 10+ messages in thread
From: Ted Dennison @ 2000-08-21  0:00 UTC (permalink / raw)


In article <87g0nyy1kk.fsf@moon.mteege.de>,
  Matthias Teege <matthias@mteege.de> wrote:
> Ted Dennison <dennison@telepath.com> writes:
> > like you are familiar with "object" notation in some other "OO"
> > languages, and are looking for the same notation in Ada. Eg: you
> > want to call Object.Find (...) rather than Find(Object, ...) Is that
> > your question?
>
> Yes, I want to call Object.Find(in_field, for_this_string)

If that's the case, then you should know that Ada does not use the
"distinguished object" notation. You will need to pass the object in
question in as a parameter. This is a FAQ, so consulting the entry at
http://www.adapower.com/lab/adafaq/16.html may help.

> but the only way to implement this I see is:
>
>         if in_field = "field1" and for_this_string = "foo"
>         then
>            result = "found";
>         if in_field = "field2" and for_this_string = "foo"
>         then
>            result = "found";
>
> and so on. The problem isn't the "object notation" but the
> record notation. I can access an record field with

Now this is beginning to look like a different question. (Again, the
above will never work, because "foo" is not 25 characters long, but
we'll ignore that issue for now). If you want the user to select which
key to use, you have several options:

   o  Make multiple "Find" routines, one for each key the user might
want to search on.

   o  Put the keys for each entry in an array in that entry, and have
the user pass in the index of the key they want to search on. You can
make this look nicer by indexing the array by an enumerated type rather
than by an integer.

   o  Change the interface to have the user pass in the value of the
key, rather than the entire object. This wouldn't work on a real linked
data structure, since each entry has its own keys. But it would suffice
for the toy example you gave above.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Searching for an object
  2000-08-21  0:00   ` Matthias Teege
  2000-08-21  0:00     ` Ted Dennison
@ 2000-08-21  0:00     ` tmoran
  2000-08-22  0:00       ` Matthias Teege
  1 sibling, 1 reply; 10+ messages in thread
From: tmoran @ 2000-08-21  0:00 UTC (permalink / raw)


>        if in_field = "field1" and for_this_string = "foo"
>        then
>           result = "found";
>        if in_field = "field2" and for_this_string = "foo"
>        then
>           result = "found";
>
>I'm looking for a short notation like
>"record.$fieldname". I hope I can make you understand. :-)
  I'm confused.  The example pseudo-code indicates there are
two fields, "in_field" and "for_this_string", while
record.$fieldname suggests, to me, that you want multiple
different field names in your record.
  If you don't like the successive "if"s, how about
   if for_this_string = "foo"
   and then ada.strings.fixed.index("field1 field2", in_field) /= 0 then
     result := "found";
(assuming, of course, that all strings are the indicated length).
  If I understand correctly what you mean by record.$fieldname, why
not just have the record contain an array of the fields?




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

* Re: Searching for an object
  2000-08-21  0:00     ` tmoran
@ 2000-08-22  0:00       ` Matthias Teege
  2000-08-22  0:00         ` tmoran
  0 siblings, 1 reply; 10+ messages in thread
From: Matthias Teege @ 2000-08-22  0:00 UTC (permalink / raw)


tmoran@bix.com writes:

> >        if in_field = "field1" and for_this_string = "foo"
> >        then
> >           result = "found";
> >        if in_field = "field2" and for_this_string = "foo"
> >        then
> >           result = "found";
> >
> >I'm looking for a short notation like
> >"record.$fieldname". I hope I can make you understand. :-)
>   I'm confused.  The example pseudo-code indicates there are
> two fields, "in_field" and "for_this_string", while
> record.$fieldname suggests, to me, that you want multiple
> different field names in your record.

No, the pseudo code deceive. I have an record type with
for instance two fields and "in_field" stands for the
fieldname and "for_this_string" stands for the value.

type Customer is 
   record 
        lastname  : String ( 1..10 );
        firstname : String ( 1..10 );
   end record

Cust : Customer;

Cust.lastname  = "Teege     ";
Cust.firstname = "Matthias  ";

Now I build an container with a lot of customers and wan't
to find one of them. For this case I look for an procedure
which I can pass two arguments. Not only the searchstring
(Teege) but also the fieldname (lastname).

A appropriate call looks like this: 

Find("lastname", "Matthias  "); -- not found
Find("lastname", "Teege     "); -- found

>   If you don't like the successive "if"s, how about

Yes, that is the problem, too many "if's". :-)

>    if for_this_string = "foo"
>    and then ada.strings.fixed.index("field1 field2", in_field) /= 0 then
>      result := "found";
> (assuming, of course, that all strings are the indicated length).
>   If I understand correctly what you mean by record.$fieldname, why
> not just have the record contain an array of the fields?

You, TED and the others gave a lot of input and I will try
to recycle some of them. ;-)

Many thanks
Matthias

-- 
Matthias Teege -- matthias@mteege.de -- http://emugs.de
make world not war
PGP-Key auf Anfrage




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

* Re: Searching for an object
  2000-08-22  0:00       ` Matthias Teege
@ 2000-08-22  0:00         ` tmoran
  0 siblings, 0 replies; 10+ messages in thread
From: tmoran @ 2000-08-22  0:00 UTC (permalink / raw)


I presume you couldn't do
  Lastname : array(1 .. N) of String(1 .. 10);
  Firstname : array(1 .. N) of String(1 .. 10);

and have separate Find_xxx routines for the different fields

Find_Lastname("Matthias  "); -- not found
Find_Lastname("Teege     "); -- found

thus making the caller of Find_xxx make the "which field" decision.

If a single Find routine needs to make a decision based on the
particular desired field, then at some level you really do need what
amounts to a set of "if"s or a "case" statement.  If you can use a
subscript and index, then the hardware that decides which RAM address
you're accessing can make the decision, eg

type fields is (lastname, firstname);
subtype names is string(1 .. 10);
type name_parts is array(fields) of names;

type Customer is
   record
        name_part : name_parts;
        other_info: other_types;
   end record

Cust : Customer;

Cust.name_part(lastname) = "Teege     ";
Cust.name_part(firstname)= "Matthias  ";

Find(lastname, "Matthias  "); -- not found
Find(lastname, "Teege     "); -- found

If you can't do that, you could get the compiler to generate an
implicit "case" by using tagged types and dispatching to a particular
Find depending on the field, but that might be overkill.




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

* Re: Searching for an object
  2000-08-18  0:00 Searching for an object Matthias Teege
  2000-08-21  0:00 ` Gerald Kasner
  2000-08-21  0:00 ` Ted Dennison
@ 2000-08-31 21:55 ` John McCabe
  2 siblings, 0 replies; 10+ messages in thread
From: John McCabe @ 2000-08-31 21:55 UTC (permalink / raw)


Matthias Teege <matthias@mteege.de> wrote:

>I've defined the following type: 
>
>type customer is record
>   aname  : String( 1..25);
>   bname  : String( 1..25);
>end record;
>
>Then I create some objects of this type and save all of
>them in an table.
>
>Now I want to find a special object with a find procedure
>like this (not authentic Ada Code :-)):
>
>procedure find ( Field : in String;
>                 What  : in String;
>                 Result: out String) is
>
>while end_of_table is false loop
>  read(object);
>  if object.Field = What then -- field should be 'aname'
>                              -- or 'bname'
>     Result := object.Field;
>     exit; -- We have found it
>  end if
>end loop;
>end find;
>
>I want to search in all Record "Fields" depend on the
>procedure parameter.
>
>How is the Ada way?

If I understand the requirement correctly, a way to do it would be to
define:

   type Comparator_Type is access function (Left  : Customer;
                                            Right : String)
                                            return Boolean;

And define

   procedure find (What : in string;
                   Comparator : in Comparator_Type;
                   Result     : out String) is
     function "=" renames Comparator.all;
   begin
     ...
     if Object = What then
     ...
   end find;

So in a client of this package you would have:

   procedure client is
      function Match_Afield (Left : other_package.customer;
                             Right : string)
                             return boolean is
      begin
         return Left.afield = right;
      end Match_Afield;

      function Match_Bfield (Left : other_package.customer;
                             Right : string)
                             return boolean is
      begin
         return Left.bfield = right;
      end Match_Bfield;
   begin

      other_package.find (what => "blah",
                          comparator => Match_Afield'access,
                          result => local_result);
   end client;

Or something like that. This would all need to be modified and syntax
checked etc. You could also make the find function a generic that is
instantiated with an "=" operation.

John

Best Regards
John McCabe <john@assen.demon.co.uk>



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

end of thread, other threads:[~2000-08-31 21:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-18  0:00 Searching for an object Matthias Teege
2000-08-21  0:00 ` Gerald Kasner
2000-08-21  0:00   ` Matthias Teege
2000-08-21  0:00 ` Ted Dennison
2000-08-21  0:00   ` Matthias Teege
2000-08-21  0:00     ` Ted Dennison
2000-08-21  0:00     ` tmoran
2000-08-22  0:00       ` Matthias Teege
2000-08-22  0:00         ` tmoran
2000-08-31 21:55 ` John McCabe

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