comp.lang.ada
 help / color / mirror / Atom feed
* String to Variable Name Mapping
@ 1996-04-11  0:00 F. Britt Snodgrass
  1996-04-13  0:00 ` Robert Dewar
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: F. Britt Snodgrass @ 1996-04-11  0:00 UTC (permalink / raw)


Is there an attribute or some other way to use the contents of a string
variable to point to another variable?

For example, if I have the following declarations,

  Int_Var : Integer;
  Str_Var : String (1..32) := "Int_Var";

can I somehow use the contents of Str_Var to make an assignment to 
Int_Var?

I'd like to do this as a way of initializing variables by reading from a 
text file containing the variable name and the desired initial value.

Thanks,
Britt Snodgrass  (mailto:britt@acm.org)





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

* Re: String to Variable Name Mapping
  1996-04-11  0:00 String to Variable Name Mapping F. Britt Snodgrass
  1996-04-13  0:00 ` Robert Dewar
@ 1996-04-13  0:00 ` Laurent Guerby
  1996-04-19  0:00 ` Todd Coniam
  1996-04-25  0:00 ` Felaco
  3 siblings, 0 replies; 5+ messages in thread
From: Laurent Guerby @ 1996-04-13  0:00 UTC (permalink / raw)


"F. Britt Snodgrass" writes
: 
: Is there an attribute or some other way to use the contents of a string
: variable to point to another variable?
: 
: For example, if I have the following declarations,
: 
:   Int_Var : Integer;
:   Str_Var : String (1..32) := "Int_Var";
: 
: can I somehow use the contents of Str_Var to make an assignment to 
: Int_Var?
: 
: I'd like to do this as a way of initializing variables by reading from a 
: text file containing the variable name and the desired initial value.

   Ada is not an interpreted language that allow such tricks ;-). Here
is a reasonable way of doing something equivalent :

--  Laurent Guerby, 13 April 1996.
--  Tested and approved by GNAT ;-).

with Ada.Text_IO;

procedure Symbol is

   package Text_IO renames Ada.Text_IO;
   --  with vs use ... ;-).

   type Variable_Type is new Integer;
   --  Taken from your example.

   package Value_IO is new 
      Ada.Text_IO.Integer_IO (Variable_Type);
   --  Easy value IO.
  
   type Variable_Name is (Var_1, Var_2, Var_3); 
   --  Enumeration type.

   package Name_IO is new 
      Ada.Text_IO.Enumeration_IO (Variable_Name);
   --  You'll need it for easy IO operations on names.

   type Table is array (Variable_Name) of Variable_Type;
   --  If you want different types, it's doable but it'll need
   --  a bit more work.

   Symbol_Table : Table;
   --  The interesting variable ;-).

   Current_Name  : Variable_Name;
   Current_Value : Variable_Type;

begin
   Name_IO.Get  (Current_Name);
   Value_IO.Get (Current_Value);
   Symbol_Table (Current_Name) := Current_Value;

   Text_IO.New_Line (2);

   Name_IO.Put  (Current_Name); 
   Text_IO.Put  (" = ");
   Value_IO.Put (Symbol_table (Current_Name));
   Text_IO.New_line;
end Symbol;

   Here is one possible execution :

guerby@leibniz:/tmp$ ./symbol 
var_1
2
 
 
VAR_1 =           2
guerby@leibniz:/tmp$

   This   make (extensive)   use  of  Ada.Text_IO, if   you  want more
precisisons   about that,  just   follow the  Lovelace tutorial  (from
http://lglwww.epfl.ch/Ada/ by  David Wheeler) or  run to your bookshop
and buy an Ada book ;-).

   Note that we use  that in DART with an  array of access to  Mapping
function,  to make the link between  the scene description file (which
uses names) and predefined color mappings (the Symbol_Table is defined
at elaboration time with procedures access).

   If you got a name clash between your enum type and a predefined Ada
word, youi've to add a dummy postfix (or prefix) to each name and hack
a bit the IO routines.

: Thanks,
: Britt Snodgrass  (mailto:britt@acm.org)

   Hope this helps,

-- 
--  Laurent Guerby, student at Telecom Bretagne (France), Team Ada
--  "Use the Source, Luke. The Source will be with you, always (GPL)"
--  http://www-eleves.enst-bretagne.fr/~guerby/ (GATO Project)
--  Try GNAT, the GNU Ada 95 compiler (ftp://cs.nyu.edu/pub/gnat)




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

* Re: String to Variable Name Mapping
  1996-04-11  0:00 String to Variable Name Mapping F. Britt Snodgrass
@ 1996-04-13  0:00 ` Robert Dewar
  1996-04-13  0:00 ` Laurent Guerby
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Robert Dewar @ 1996-04-13  0:00 UTC (permalink / raw)


Britt asks

"For example, if I have the following declarations,

  Int_Var : Integer;
  Str_Var : String (1..32) := "Int_Var";

can I somehow use the contents of Str_Var to make an assignment to
Int_Var?"

No, and if you think about it a bit (visibility issues, safety issues,
runtime efficiency issues -- any one would be sufficient), such a feature
is out of the question in Ada. Some very dynamic languages like SNOBOL-4
and LISP have this feature.






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

* Re: String to Variable Name Mapping
  1996-04-11  0:00 String to Variable Name Mapping F. Britt Snodgrass
  1996-04-13  0:00 ` Robert Dewar
  1996-04-13  0:00 ` Laurent Guerby
@ 1996-04-19  0:00 ` Todd Coniam
  1996-04-25  0:00 ` Felaco
  3 siblings, 0 replies; 5+ messages in thread
From: Todd Coniam @ 1996-04-19  0:00 UTC (permalink / raw)


In article <4kjico$emb@newssvr.cacd.rockwell.com>, fbsnodgr@cacd.rockwell.com 
says...
>
>Is there an attribute or some other way to use the contents of a string
>variable to point to another variable?
>
>For example, if I have the following declarations,
>
>  Int_Var : Integer;
>  Str_Var : String (1..32) := "Int_Var";
>
>can I somehow use the contents of Str_Var to make an assignment to 
>Int_Var?
>
>I'd like to do this as a way of initializing variables by reading from a 
>text file containing the variable name and the desired initial value.
>
>Thanks,
>Britt Snodgrass  (mailto:britt@acm.org)
>

-- 

Yes, but it involves writing a small parser that determines which variable is 
listed in the file, reads the value, and assigns it to the appropriate actual 
variable.

What your wanting is not directly supported by Ada as it is in some other 
languages.  It is not considered "safe" (which I agree with) but I do see the 
utility of your need (it's a initialization file, and done all the time).

Sorry, there is no fast answer.  If you are having problems creating the 
necessary procedure let us (c.l.a) know and we can help you out.

-------------------------------------------------------------------------
Todd Coniam       | Member: Team Ada
tconiam@ionet.net | Ada 95 - The international standard in OO languages
-------------------------------------------------------------------------
Check: http://lglwww.epfl.ch/Ada/     Free compiler: http://www.gnat.com/





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

* Re: String to Variable Name Mapping
  1996-04-11  0:00 String to Variable Name Mapping F. Britt Snodgrass
                   ` (2 preceding siblings ...)
  1996-04-19  0:00 ` Todd Coniam
@ 1996-04-25  0:00 ` Felaco
  3 siblings, 0 replies; 5+ messages in thread
From: Felaco @ 1996-04-25  0:00 UTC (permalink / raw)


F. Britt Snodgrass (fbsnodgr@cacd.rockwell.com) wrote:
: Is there an attribute or some other way to use the contents of a string
: variable to point to another variable?

: For example, if I have the following declarations,

:   Int_Var : Integer;
:   Str_Var : String (1..32) := "Int_Var";

: can I somehow use the contents of Str_Var to make an assignment to 
: Int_Var?

: I'd like to do this as a way of initializing variables by reading from a 
: text file containing the variable name and the desired initial value.

The answer is no, there is no way to map an arbitrary string to an 
arbitrary variable name.  However, you can convert strings to enumeration 
literals and vice versa.  With a little more programming, you can define 
all of your user defined variables in an array indexed by the variable 
name.  The "variable name" will be an enumerated type you defined.  You 
can use Text_Io.Enumeration_Io to read in the variable names, or just use 
the 'Value attribute of the enumerated type you defined.

Anyway, I don't know too much about it, but I have read that Tcl was 
designed specifically for this purpose.  I wonder if anyone has set up 
Ada bindings for Tcl or maybe even ported it entirely?

--
-------------------------------------------------------------------------------
Chris Felaco                               Phone: x4631 (Raynet 444, Local 842)
Raytheon Company                                         Email: bcf@ssd.ray.com
-------------------------------------------------------------------------------




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

end of thread, other threads:[~1996-04-25  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-04-11  0:00 String to Variable Name Mapping F. Britt Snodgrass
1996-04-13  0:00 ` Robert Dewar
1996-04-13  0:00 ` Laurent Guerby
1996-04-19  0:00 ` Todd Coniam
1996-04-25  0:00 ` Felaco

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