473,804 Members | 4,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating names of variables at run-time

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
Apr 1 '08 #1
6 2429
Helmut Giese wrote:
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[i]] = "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"');

--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 1 '08 #2
On Apr 1, 2:24 pm, Martin Honnen wrote:
<snip>
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 -.
Apr 1 '08 #3
On Apr 1, 10:10 am, Helmut Giese wrote:
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.
Apr 1 '08 #4
Hi Erwin,
>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.
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
Apr 1 '08 #5
>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[i]] = "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
Apr 1 '08 #6
DS
On Tue, 01 Apr 2008 11:10:09 +0200, Helmut Giese wrote:
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.
Hi, why are you converting 50k lines of presumably fully-working bug-free
tcl code?
And why javascript in particular?
Why not ruby, python or lua etc?
You might be better served leaving the tcl code as-is and extending your
codebase with javascript objects as needed.
Apr 4 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1823
by: Ron_Adam | last post by:
Hi, Sometimes it just helps to see what's going on, so I've been trying to write a tool to examine what names are pointing to what objects in the current scope. This still has some glitches, like not working in winpython or the command line, I get a 'stack not deep enough' error. I haven't tested it on linux yet either. It could be something with my python install.(?) Anyway, here's the source with an example output. I'm still not...
20
3128
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then alert(a2) I will see 42 there too. I'm doubting, but I was
22
3158
by: Tom Moroow | last post by:
Hi, I'm pretty new to javascript and was wondering how you would piece together a variable name and then assign it a value. I want to create a hidden field and assign it a value based on the value of another variable Right now it looks like: ("document.all.SM_MARK_10" + dateNumber + ".value") where dateNumber is an already defined integer. What I want is to say
11
16300
by: randi_clausen | last post by:
Using SQL against a DB2 table the 'with' key word is used to dynamically create a temporary table with an SQL statement that is retained for the duration of that SQL statement. What is the equivalent to the SQL 'with' using TSQL? If there is not one, what is the TSQL solution to creating a temporary table that is associated with an SQL statement? Examples would be appreciated. Thank you!!
15
2271
by: James | last post by:
Hi, I am finding it increasingly difficult to name my variables. I am not able to think in the right way. Expert C programmers please Help. Regards,
6
6165
by: Marcus Kwok | last post by:
I am designing a GUI (my question is not about GUIs) and I have named my variables using a regular naming scheme. However, in order to simplify the code using these variables, I have created an array of non-owning pointers to these variables. I am trying to write a macro to generate these variable names for me, but I am not sure if what I want to do is possible. The code below demonstrates what I want to do (except it is generating...
2
1561
by: Dave Monk | last post by:
Hi, I'm reasonably proficient in PHP but have been asked how to do something which has got me stumped. Hence, my posting: I want to create a small number of variables, $pos1, $pos2 ... $pos5 within a loop and assign them values from an array, but creating the variable names with incrementing numbers, well I've no idea how the PHP syntax for it works.
5
2887
by: Greg Corradini | last post by:
Hello All, I'm attempting to create multiple dictionaries at once, each with unique variable names. The number of dictionaries i need to create depends on the length of a list, which was returned from a previous function. The pseudo code for this problem would be: returnedlist = count = 0 for i in returnedlist: if count < len(returnedlist):
3
2157
by: Thomas Jansson | last post by:
Dear all I am writing a program with tkinter where I have to create a lot of checkbuttons. They should have the same format but should have different names. My intention is to run the functions and the create all the buttons with the names from the list. I now the lines below doesn't work, but this is what I have so far. I don't really know how call the element in the dict use in the for loop. I tried to call +'item'+ but this doesn't...
14
2116
by: tdahsu | last post by:
I have twenty-five checkboxes I need to create (don't ask): self.checkbox1 = ... self.checkbox2 = ... .. .. .. self.checkbox25 = ... Right now, my code has 25 lines in it, one for each checkbox, since
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.