473,837 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Store variable name in another variable

I need to store a list of variable names in a dictionary or list. I
then later need to retrieve the names of the variables and get the
values from the named variables. The named variables will already have
been created and given a value.

I hope thats clear!!!

How can I do this?

Apr 26 '07 #1
7 2533
loial wrote:
I need to store a list of variable names in a dictionary or list. I
then later need to retrieve the names of the variables and get the
values from the named variables. The named variables will already have
been created and given a value.

I hope thats clear!!!

How can I do this?
Use a dictionary. Example:

var1=10
var2="abc"
var2=1.2

vardict['var1']=var1
vardict['var2']=var2
vardict['var3']=var3

print vardict['var1']
print vardict['var2']
print vardict['var3']

-Larry
Apr 26 '07 #2
loial a écrit :
I need to store a list of variable names in a dictionary or list. I
then later need to retrieve the names of the variables and get the
values from the named variables. The named variables will already have
been created and given a value.
"Named variables will already have been created"... in what namespace ?

Store a list of names -dict/list (see tutorial)

Then,
use namespace.name
or getattr(namespa ce,"name")
or locals()["name"]
or globals()["name"]
Apr 26 '07 #3
In article <f0**********@n ews2.u-psud.fr>,
Laurent Pointal <la************ *@limsi.frwrote :
>loial a écrit :
>I need to store a list of variable names in a dictionary or list. I
then later need to retrieve the names of the variables and get the
values from the named variables. The named variables will already have
been created and given a value.

"Named variables will already have been created"... in what namespace ?

Store a list of names -dict/list (see tutorial)

Then,
use namespace.name
or getattr(namespa ce,"name")
or locals()["name"]
or globals()["name"]

admin, I want to be sure you understand the advice you've been given.
Yes, it is possible to "double dereference" through named variables;
HOWEVER, it is essentially *never* advantageous to do so in application
programming with Python (nor is it in Perl and PHP, despite what many
senior people there teach). Use a dictionary, perhaps one with
multi-dimensional keys.
Apr 26 '07 #4
Cameron Laird wrote:
In article <f0**********@n ews2.u-psud.fr>,
Laurent Pointal <la************ *@limsi.frwrote :
>>loial a écrit :
>>I need to store a list of variable names in a dictionary or list. I
then later need to retrieve the names of the variables and get the
values from the named variables. The named variables will already have
been created and given a value.

"Named variables will already have been created"... in what namespace ?

Store a list of names -dict/list (see tutorial)

Then,
use namespace.name
or getattr(namespa ce,"name")
or locals()["name"]
or globals()["name"]


admin, I want to be sure you understand the advice you've been given.
Yes, it is possible to "double dereference" through named variables;
HOWEVER, it is essentially *never* advantageous to do so in application
programming with Python (nor is it in Perl and PHP, despite what many
senior people there teach). Use a dictionary, perhaps one with
multi-dimensional keys.
Yes, I understand. I just reply to OP question not searching the reason why
he wants to manage *variables* this way (he talk about dict, so I hope he
know that they can be used to map names to values).
So, this is not an advice, just technical ways related to the question as it
was written.

And yes, personnally i use dictionnaries for such purpose.

A+

Laurent.

Apr 26 '07 #5
OK, I have it working with dictionaries.

However I have another scenerio :

I am reading a file containing records like the following :

<IMPOSITION_DOC UMENT>
<WFBAN_PAGE>
<WFBAN_START_DE TAILS name=\""MYVARIA BLE"\">
...
...
I need to substitute MYVARIABLE with the current value of MYVARIABLE
in my python script and write the file out again.

The file may contain many more lines and many substitution values on
any line

Assuming that MYVARIABLE is currently set to JOHN then the output
would be

<IMPOSITION_DOC UMENT>
<WFBAN_PAGE>
<WFBAN_START_DE TAILS name="JOHN">

Can this be done in Python? Amending the way the variable names are
distinguished in the incoming file is possible if that would help.



On 26 Apr, 22:02, Laurent Pointal <laurent.poin.. .@wanadoo.frwro te:
Cameron Laird wrote:
In article <f0q89v$ug...@n ews2.u-psud.fr>,
Laurent Pointal <laurent.poin.. .@limsi.frwrote :
>loial a ?it :
I need to store a list ofvariablenames in a dictionary or list. I
then later need to retrieve the names of the variables and get the
values from the named variables. The named variables will already have
been created and given a value.
>"Named variables will already have been created"... in what namespace ?
>Store a list of names -dict/list (see tutorial)
>Then,
use namespace.name
or getattr(namespa ce,"name")
or locals()["name"]
or globals()["name"]
admin, I want to be sure you understand the advice you've been given.
Yes, it is possible to "double dereference" through named variables;
HOWEVER, it is essentially *never* advantageous to do so in application
programming with Python (nor is it in Perl and PHP, despite what many
senior people there teach). Use a dictionary, perhaps one with
multi-dimensional keys.

Yes, I understand. I just reply to OP question not searching the reason why
he wants to manage *variables* this way (he talk about dict, so I hope he
know that they can be used to map names to values).
So, this is not an advice, just technical ways related to the question as it
was written.

And yes, personnally i use dictionnaries for such purpose.

A+

Laurent.- Hide quoted text -

- Show quoted text -

May 1 '07 #6
On May 1, 6:43 am, loial <a...@loial.co. ukwrote:
OK, I have it working with dictionaries.

However I have another scenerio :

I am reading a file containing records like the following :

<IMPOSITION_DOC UMENT>
<WFBAN_PAGE>
<WFBAN_START_DE TAILS name=\""MYVARIA BLE"\">
..
..

I need to substitute MYVARIABLE with the current value of MYVARIABLE
in my python script and write the file out again.

The file may contain many more lines and many substitution values on
any line

Assuming that MYVARIABLE is currently set to JOHN then the output
would be

<IMPOSITION_DOC UMENT>
<WFBAN_PAGE>
<WFBAN_START_DE TAILS name="JOHN">

Can this be done in Python?

s = "hello world, goodbye world"
result = s.replace("worl d", "moon")
print result

May 1 '07 #7
loial schrieb:
OK, I have it working with dictionaries.

However I have another scenerio :

I am reading a file containing records like the following :

<IMPOSITION_DOC UMENT>
<WFBAN_PAGE>
<WFBAN_START_DE TAILS name=\""MYVARIA BLE"\">
..
..
I need to substitute MYVARIABLE with the current value of MYVARIABLE
in my python script and write the file out again.

The file may contain many more lines and many substitution values on
any line

Assuming that MYVARIABLE is currently set to JOHN then the output
would be

<IMPOSITION_DOC UMENT>
<WFBAN_PAGE>
<WFBAN_START_DE TAILS name="JOHN">

Can this be done in Python? Amending the way the variable names are
distinguished in the incoming file is possible if that would help.
There are many ways to do so in python. You can use the built-in string
interpolation:

"<tag value="%(name)s "/>" % dict(name=value )

Or you can use one of the many available templating engines, like KID,
genshi, cheetah and so on.

Diez
May 1 '07 #8

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

Similar topics

11
2552
by: Colin Steadman | last post by:
Hope this makes sense! I'm building an ASP page which allows uses to add items to an invoice via a form, ie: Item No Part No Order No Quanity Units Price VAT ------- ------- -------- ------- ----- ----- --- .... ... ... ... ... ... ... <Add item> <Submit>
9
1941
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using top.A.tableVar = document.getElementById("someTable"); As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
12
2391
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the graphics object of the form/pictureBox. Should It be better if make a graphics object from my pictureBox in load event handler of the form and store it as member variable of the form , make
12
2281
by: harishg2 | last post by:
Hi, How to store a variable value for more than one executions. Ex: main() { int i=0; i++; printf("%d",i);
2
1391
by: Geert M | last post by:
Is it possible to store collections of my custom defined object in an application state variable. How can this be done?
36
2872
by: Crirus | last post by:
Hi I have about 1000 records of users to store.. I dont like the ideea of a database server... How to store does data in a structured way? An XML? How big can became such an xml, because I whould like to load it and search for data. Can I search for some data without loading all xml into memory? --
6
2047
by: Mike Hoff | last post by:
I am trying to write a class that will store info about database fields for building UPDATE / INSERT commands later on. I cannot seem to get the sytax correct to pass and store the data type of the field. My class looks like: Public Class DataTag Private m_Text As String Private m_FieldName As String Private m_ParamName As String Private m_DataType As System.Type 'the variable type of this field
11
4383
by: c676228 | last post by:
Hi everyone, I am just wodering in asp program, if there is anybody writing store procedure for inserting data into database since there are so many parameters need to be passed into store procedure(assume there are many columns in the table). I need to insert data into two separate tables, the relation between these two tables is 1 row of data in table1 could have multiple rows in table2 related to table1, but if the data insertion into...
7
5732
by: John Smith | last post by:
Hello, I have a simple question, I have a vb.net form with several buttons. If I store the name of a button in a variable.. Dim TheName as string TheName = Me.btnMyLittleButton.Name.ToString How can I disable this button using the variable value?
0
9683
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10882
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
10577
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
10275
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
7811
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
7005
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
5670
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...
0
5851
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.