comp.lang.ada
 help / color / mirror / Atom feed
* Array index ?
@ 1998-01-29  0:00 SiliconJesus
  1998-01-29  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 3+ messages in thread
From: SiliconJesus @ 1998-01-29  0:00 UTC (permalink / raw)



I am trying to write some code that allows me to make an array of records
and I want the array to have index for a string of 4,so Array(Sock) like a
warehouse storage type program, I know this should be trivial to me, but
I've been coding in C++ and Java for a semester and have forgotten some of
my Ada roots, any help would be appreciated.

Thanks,

John

		
				,.   '\'\    ,---.
				| \\  l\\l_ //    |     ~NARF!!
				|  \\/ `/  `.|    |      
				| Y |   |   ||  Y |      
				|  \|   |   |\ /  |      
				\   |  o|o  | >  /       
				 \___\_--_ /_/__/        
				 /.-\(____) /--.\        
				 `--(______)----'
				     U// U / \
				     / \  / /|           		   
==========================================================================
|    John M. Softich                | " I wanna live, I wanna love       |
|  softichj@cs.montana.edu          |      but it's a long hard          |
|  Montana State University         |       road out of hell! "          |
|  Http://cs.montana.edu/~softichj  |                ~Manson             |
==========================================================================





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

* Re: Array index ?
  1998-01-29  0:00 Array index ? SiliconJesus
@ 1998-01-29  0:00 ` Matthew Heaney
  1998-01-31  0:00   ` Nick Roberts
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Heaney @ 1998-01-29  0:00 UTC (permalink / raw)



In article <Pine.OSF.3.95.980129004337.31543A-100000@esus.cs.montana.edu>,
SiliconJesus <softichj@cs.montana.edu> wrote:

>I am trying to write some code that allows me to make an array of records
>and I want the array to have index for a string of 4,so Array(Sock) like a
>warehouse storage type program, I know this should be trivial to me, but
>I've been coding in C++ and Java for a semester and have forgotten some of
>my Ada roots, any help would be appreciated.

Do you mean you want an array whose index subtype is a subtype of String, as in

subtype T_Index is String (1 .. 4);

type T is array (T_Index) of R;

If so, you can't do that, because arrays can only have a discrete subtype
(integer or enumeration type) as the index subtype.  

You probably want a more abstract data structure, such as a dictionary
(sort of like a symbol table - consult you fave compiler book) or a
trie-structure (not a misspell) or a hash table, etc.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: Array index ?
  1998-01-29  0:00 ` Matthew Heaney
@ 1998-01-31  0:00   ` Nick Roberts
  0 siblings, 0 replies; 3+ messages in thread
From: Nick Roberts @ 1998-01-31  0:00 UTC (permalink / raw)



One simple scheme might be to do a simple conversion from the string
T_Index to a discrete type, an integer type T_Int_Index say.  Supposing the
string will only ever contain the digits 0 to 9, the following function
would do the conversion:

   type T_Int_Index is Integer range 0..9999;

   function To_Int(S: T_Index) return T_Int_Index is
   begin
      return S(1)*1000 + S(2)*100 + S(3)*10 + S(4);
   end;

The array could then be declared as:

   type T is array(T_Int_Index) of R;

then an array, such as

   Stock: T;

could be accessed by an index N like this:

   Q1 := Q1 + Stock(To_Int(N)).Quant_In;

This is a simple example of 'hashing'.  The disadvantage of this simple
system may be that the array, which has 10000 elements, may be much too big
(in that most of it remains unsed).  If this is the case, you might be able
to get around the problem with a more complicated 'hashing index' -- the
function defined above.  Have fun!

-- 

== Nick Roberts ================================================
== Croydon, UK                       ===========================
==                                              ================
== Proprietor, ThoughtWing Software                   ==========
== Independent Software Development Consultant            ======
== Nick.Roberts@dial.pipex.com                              ====
== Voicemail & Fax +44 181-405 1124                          ===
==                                                            ==
==           I live not in myself, but I become               ==
===          Portion of that around me; and to me             ==
====         High mountains are a feeling, but the hum        ==
=======      Of human cities torture.
===========                             -- Byron [Childe Harold]


Matthew Heaney <mheaney@ni.net> wrote in article
<mheaney-ya023680002901982253280001@news.ni.net>...
> Do you mean you want an array whose index subtype is a subtype of String,
as in
> 
> subtype T_Index is String (1 .. 4);
> 
> type T is array (T_Index) of R;
> 
> If so, you can't do that, because arrays can only have a discrete subtype
> (integer or enumeration type) as the index subtype.  
> 
> You probably want a more abstract data structure, such as a dictionary
> (sort of like a symbol table - consult you fave compiler book) or a
> trie-structure (not a misspell) or a hash table, etc.





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

end of thread, other threads:[~1998-01-31  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-29  0:00 Array index ? SiliconJesus
1998-01-29  0:00 ` Matthew Heaney
1998-01-31  0:00   ` Nick Roberts

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