String to Variable Name Mapping 
Author Message
 String to Variable Name Mapping

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,



Mon, 28 Sep 1998 03:00:00 GMT  
 String to Variable Name Mapping
"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 :


var_1
2

VAR_1 =           2

   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,

   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)



Wed, 30 Sep 1998 03:00:00 GMT  
 String to Variable Name Mapping
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.



Wed, 30 Sep 1998 03:00:00 GMT  
 String to Variable Name Mapping

says...

Quote:

>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,


--

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

-------------------------------------------------------------------------
Check: http://lglwww.epfl.ch/Ada/     Free compiler: http://www.gnat.com/



Tue, 06 Oct 1998 03:00:00 GMT  
 String to Variable Name Mapping

: 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)

-------------------------------------------------------------------------------



Mon, 12 Oct 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Using a variable name for a variable name??

2. Procedure name in a string variable

3. Using variable names as strings

4. treating variable names as strings (NEWBIE)

5. Variable names in text strings

6. Variable name -> string

7. creating variable names by adding 2 strings?

8. how to convert variable name to a string?

9. string to variable name

10. More About Array Names In String Variables

11. field name and key name as a variable

12. field name and key name as a variable

 

 
Powered by phpBB® Forum Software