Creating names of variables at run-time

H

Helmut Giese

Hello out there,
I am new to JS so please bear with me.

I am tasked to investigate, whether it is feasible to port one of our
applications from Tcl to JS - the idea being to write a program to do
the conversion, mapping language constructs from Tcl to equivalent
constructs in JS.
If we pursue this project I will surely come here more often :) but
for a starter I have this question:
Tcl syntax:
set i 1 // equivalent to 'var i = 1;'
puts "$i" // prints '1' - $i returning the current value of 'i'

In Tcl one can create names of variables at run-time, like
set i 1
set a$i "some string"
the last statement creating a variable with the name 'a1' and the
value "some string".
Question: How do I achieve this in JS?

Any insight will be greatly appreciated.
Best regards
Helmut Giese
 
E

Erwin Moller

Helmut Giese schreef:
Hello out there,
I am new to JS so please bear with me.

I am tasked to investigate, whether it is feasible to port one of our
applications from Tcl to JS - the idea being to write a program to do
the conversion, mapping language constructs from Tcl to equivalent
constructs in JS.
If we pursue this project I will surely come here more often :) but
for a starter I have this question:
Tcl syntax:
set i 1 // equivalent to 'var i = 1;'
puts "$i" // prints '1' - $i returning the current value of 'i'

In Tcl one can create names of variables at run-time, like
set i 1
set a$i "some string"
the last statement creating a variable with the name 'a1' and the
value "some string".
Question: How do I achieve this in JS?

Any insight will be greatly appreciated.
Best regards
Helmut Giese

Hi Helmut,

I think I would store them in an object, like this:

<script type="text/javascript">
var myObj = new Object();
myObj["bla"] = 23;
myObj["bla2"] = new Array(1,4,89);
alert(myObj["bla"]);
</script>

Used that way Objects in JavaScript mimic the concept of hashed
list/associative arrays/whatever they are called in TCL.

In above example "bla" and "bla2" are strings, and could be replaced by
your variablenames.

Hope that helps.

Regards,
Erwin Moller
 
H

Helmut Giese

Helmut Giese schreef:
Hello out there,
I am new to JS so please bear with me.

I am tasked to investigate, whether it is feasible to port one of our
applications from Tcl to JS - the idea being to write a program to do
the conversion, mapping language constructs from Tcl to equivalent
constructs in JS.
If we pursue this project I will surely come here more often :) but
for a starter I have this question:
Tcl syntax:
set i 1 // equivalent to 'var i = 1;'
puts "$i" // prints '1' - $i returning the current value of 'i'

In Tcl one can create names of variables at run-time, like
set i 1
set a$i "some string"
the last statement creating a variable with the name 'a1' and the
value "some string".
Question: How do I achieve this in JS?

Any insight will be greatly appreciated.
Best regards
Helmut Giese

Hi Helmut,

I think I would store them in an object, like this:

<script type="text/javascript">
var myObj = new Object();
myObj["bla"] = 23;
myObj["bla2"] = new Array(1,4,89);
alert(myObj["bla"]);
</script>

Used that way Objects in JavaScript mimic the concept of hashed
list/associative arrays/whatever they are called in TCL.

In above example "bla" and "bla2" are strings, and could be replaced by
your variablenames.
Hi Erwin,
thanks for the fast response, although it doesn't really make me happy
:)
The goal - if we decide to pursue this route - is to do _machine
translation_. For a tiny example consider this Tcl code
set a 1
set b [foo $a]
which would be translated to
var a, b;
a = 1;
b = foo(a);
It seems feasable to have a program do this translation.

If I have to introduce objects in those situations which go beyond the
simple example from above, things get - well - interesting. Currently
I don't really see a way to handle this _without_ human intervention.

If you have any other idea don't hesitate to post it :)
Thanks and best regards
Helmut Giese
 
M

Martin Honnen

Helmut said:
In Tcl one can create names of variables at run-time, like
set i 1
set a$i "some string"
the last statement creating a variable with the name 'a1' and the
value "some string".
Question: How do I achieve this in JS?

Variables in JavaScript are properties of an object and properties can
be accessed with both dot and bracket notation where bracket notation
allows you to concatenate the name as an expression:
var g = this;
g['i'] = 1;
g['a' + g] = "some string";
That would only work for global variables however as there the global
object is accessible.
Other than that there is eval:
eval('a' + i + ' = ' + '"some string"');
 
H

Henry

On Apr 1, 2:24 pm, Martin Honnen wrote:
That would only work for global variables however as
there the global object is accessible.
Other than that there is eval:
eval('a' + i + ' = ' + '"some string"');

eval('var a' + i + ' = ' + '"some string"');

- for the runtime creation of function local variables. But in a
machine translation context it might be effective when these variables
had restricted scope to inset an object on the scope chain with a -
with - statement and add properties to that. The overheads would not
necessarily be different/worse than using - eval -.
 
H

Henry

Hello out there,
I am new to JS so please bear with me.

I am tasked to investigate, whether it is feasible to port
one of our applications from Tcl to JS
<snip>

Feasible rather than possible because it must be possible by may be an
unrealistic effort. As it is possible to write a javascript engine in
javascript it is should also be possible to write a Tcl interpreter/
engine in javascript. I don't know Tcl which makes it impossible for
me to judge the effort involved, and that will be a problem when
asking questions here as that may also be true of a large proportion
of the contributors to this group. For example, when you ask about the
runtime creation of variables I wondered about Tcl's variable scoping
(and how it would relate to how javascript handles variables (lexical
scoping at function units)). So when asking questions in relation to
this it may be a good idea to go overboard on the expatiations of what
Tcl does (and how it behaves). Unfortunately it would also be a good
idea to express those explanations in relation to javascript, but if
you are new to javascript that may not be that practical.
 
E

Erwin Moller

Helmut Giese schreef:
Helmut Giese schreef:
Hello out there,
I am new to JS so please bear with me.

I am tasked to investigate, whether it is feasible to port one of our
applications from Tcl to JS - the idea being to write a program to do
the conversion, mapping language constructs from Tcl to equivalent
constructs in JS.
If we pursue this project I will surely come here more often :) but
for a starter I have this question:
Tcl syntax:
set i 1 // equivalent to 'var i = 1;'
puts "$i" // prints '1' - $i returning the current value of 'i'

In Tcl one can create names of variables at run-time, like
set i 1
set a$i "some string"
the last statement creating a variable with the name 'a1' and the
value "some string".
Question: How do I achieve this in JS?

Any insight will be greatly appreciated.
Best regards
Helmut Giese
Hi Helmut,

I think I would store them in an object, like this:

<script type="text/javascript">
var myObj = new Object();
myObj["bla"] = 23;
myObj["bla2"] = new Array(1,4,89);
alert(myObj["bla"]);
</script>

Used that way Objects in JavaScript mimic the concept of hashed
list/associative arrays/whatever they are called in TCL.

In above example "bla" and "bla2" are strings, and could be replaced by
your variablenames.
Hi Erwin,

Hi Helmut,
thanks for the fast response, although it doesn't really make me happy
:)
The goal - if we decide to pursue this route - is to do _machine
translation_. For a tiny example consider this Tcl code
set a 1
set b [foo $a]
which would be translated to
var a, b;
a = 1;
b = foo(a);
It seems feasable to have a program do this translation.

What you above is working Javascript code.
So if that is a solution for you, just do it that way (without an object).

If I have to introduce objects in those situations which go beyond the
simple example from above, things get - well - interesting. Currently
I don't really see a way to handle this _without_ human intervention.

If you have any other idea don't hesitate to post it :)

To be honest, I think you might get yourself in all kind of JavaScript
problems since you don't know too much about JavaScript.

Henry wrote already about scope, which is important to understand.
eg:
// i is now global
var i = 42;

function test(){
// when using the var keyword inside a
//function declares it local for the function.
var i = 43;
alert(i);
}

alert ("i before:"+i);
test();
alert ("i after:"+i);

That should give you 3 alerts, with 42, 43, 42

If you ommit the var keyword inside the test function, you acces the
global i, and overwrite 42 with 43.

This is just an example. You must know about such things I expect,
before writing a TCL->JS translator.

I am inclined to advise you to write the JavaScript from scratch.
Maybe hire a JavaScript guy/girl and learn him him/her how to read TCL.
It depends on how much time you want to invest yourself to learn
Javascript of course. ;-)

Good luck!

Regards,
Erwin Moller
 
H

Helmut Giese

Hi Erwin,
To be honest, I think you might get yourself in all kind of JavaScript
problems since you don't know too much about JavaScript.
you are very polite. I know next to nothing about JS and I _will_ get
me into all kinds of trouble :) - but it is my job to find a realistic
answer to the question "to port or not to port".

[snip example of scope]
This is just an example. You must know about such things I expect,
before writing a TCL->JS translator.
Thanks for the hint, but I am aware of the differences between
different languages how to handle name lookup.
I am inclined to advise you to write the JavaScript from scratch.
Maybe hire a JavaScript guy/girl and learn him him/her how to read TCL.
Never, no way. We are talking about some 50.000 lines of code here,
maybe more.
It depends on how much time you want to invest yourself to learn
Javascript of course. ;-)
Couple o' days? :)) Might depend a bit on the kind of advice I can
expect from this newsgroup :) - but so far I am positively surprised.
Thanks again and keep up the good work - I might^H^H^H^H^H will be
back.
Best regards
Helmut Giese
 
H

Helmut Giese

Variables in JavaScript are properties of an object and properties can
be accessed with both dot and bracket notation where bracket notation
allows you to concatenate the name as an expression:
var g = this;
g['i'] = 1;
g['a' + g] = "some string";
That would only work for global variables however as there the global
object is accessible.
Other than that there is eval:
eval('a' + i + ' = ' + '"some string"');

Thanks Martin,
this news group seems to be really helpful. Combining the advice of
the 3 of you it looks like there is a way.
Best regards
Helmut Giese
 
H

Helmut Giese

<snip>

Feasible rather than possible because it must be possible by may be an
unrealistic effort. As it is possible to write a javascript engine in
javascript it is should also be possible to write a Tcl interpreter/
engine in javascript. I don't know Tcl which makes it impossible for
me to judge the effort involved, and that will be a problem when
asking questions here as that may also be true of a large proportion
of the contributors to this group. For example, when you ask about the
runtime creation of variables I wondered about Tcl's variable scoping
(and how it would relate to how javascript handles variables (lexical
scoping at function units)). So when asking questions in relation to
this it may be a good idea to go overboard on the expatiations of what
Tcl does (and how it behaves). Unfortunately it would also be a good
idea to express those explanations in relation to javascript, but if
you are new to javascript that may not be that practical.
Hi Henry,
thanks for your thoughts.
I was trying to be as concise as possible - trying to reduce the
question to its minimal content. Of course there is more - a lot more
the more I think about it.
'scope' has to be taken into account, since Tcl's scoping rules are
different from JS's, but I think I can handle this. Actually it
shouldn't be too difficult (conceptually speaking):
1) Tcl has no nested functions, so variables are either local or
global (let's ignore Tcl's namespaces for a moment).
2) A nice thing (in this context): Global variables have to be
"imported" into any function in order to be accessable there.
3) There is no 'var' declaration in Tcl - a 'set' statement defines a
variable by giving it its initial value.

So the algorithm would go like this:
1) On entry into a function collect all imported global variables.
2a) A variable is 'set' and it is not global: Then it is a local
variable and needs to appear in a 'var' statement - details may vary
according to final implementation.
2b) One of the imported variables is either 'set' or used: Don't do
anything special since it exists already in outer scope.

As for the creation of local variables: It seems like I have several
options even for the "complicated" case (and in the beginning I
wondered if I had any):
1a) 'set i 1' -> var i = 1;
1b) set a$i "some string" -> eval('var a' + i + ' = ' + '"some
string"');
'eval' may be slow (it often is in dynamic languages because it isn't
compiled - don't know about JS, though), but then this "set a$i" stuff
is much rarer than the plain "set i 1".

Maybe the following is also an option: Create a hash table for all
local variables:
var localVar ();
The simple case: localVar['i'] = 1;
The 'involved' case: localVar['a' + localVar] = "some string";
(could be the syntax is wrong, never mind, I'm talking concept right
now).
In any case, this thread was quite helpful and gives me stuff to try
out and think about.
Thanks again and best regards
Helmut Giese
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top