473,221 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

dinmaic variable name

Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?

Jul 3 '06 #1
14 1267
ey*******@gmail.com wrote:
Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?
No, but it sounds like you should store the values in an array.

--
Ian Collins.
Jul 3 '06 #2

Ian Collins wrote:
ey*******@gmail.com wrote:
Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?
No, but it sounds like you should store the values in an array.

--
Ian Collins.
Thanks
An array will be a good idea however I am getting these values from a
DB table into a struct with a lot of other fields
Is there a way to transform this struct? Or maybe some other way?

Jul 3 '06 #3
ey*******@gmail.com wrote:
Ian Collins wrote:
>>ey*******@gmail.com wrote:
>>>Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?

No, but it sounds like you should store the values in an array.

--
Ian Collins.


Thanks
An array will be a good idea however I am getting these values from a
DB table into a struct with a lot of other fields
Is there a way to transform this struct? Or maybe some other way?
That depends on what the C wrapper to your database returns. Those I
have used return an array for each database row, so you should be able
to map to another array.

Is your code populating the struct?

--
Ian Collins.
Jul 3 '06 #4

Ian Collins wrote:
ey*******@gmail.com wrote:
Ian Collins wrote:
>ey*******@gmail.com wrote:

Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?
No, but it sounds like you should store the values in an array.

--
Ian Collins.

Thanks
An array will be a good idea however I am getting these values from a
DB table into a struct with a lot of other fields
Is there a way to transform this struct? Or maybe some other way?
That depends on what the C wrapper to your database returns. Those I
have used return an array for each database row, so you should be able
to map to another array.

Is your code populating the struct?

--
Ian Collins.
The SQL that I am running on the DB to fetch the row, fetches it into
the struct. So the struct is being populated by the SQL.

Jul 3 '06 #5

Ian Collins 写道:
ey*******@gmail.com wrote:
Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?
No, but it sounds like you should store the values in an array.

--
Ian Collins.
Jul 3 '06 #6
ey*******@gmail.com wrote:
Ian Collins wrote:
>>ey*******@gmail.com wrote:
>>>Ian Collins wrote:
ey*******@gmail.com wrote:
>Hi
>let's say I have 20 variables named a1-a20 which are all of the same
>type (int)
>I need to access each one of them and according to the value do some
>operation.
>Is there some way I can access them inside a loop and build there name
>dinamically or do I have to access each one of them seperatly?
>

No, but it sounds like you should store the values in an array.
Thanks
An array will be a good idea however I am getting these values from a
DB table into a struct with a lot of other fields
Is there a way to transform this struct? Or maybe some other way?

That depends on what the C wrapper to your database returns. Those I
have used return an array for each database row, so you should be able
to map to another array.

Is your code populating the struct?

The SQL that I am running on the DB to fetch the row, fetches it into
the struct. So the struct is being populated by the SQL.
Please adjust your news reader so it does not quote signatures (the bit
under the --). This is normally a default setting.

If the DB (which one?) gives you a struct, you will have to do the
mapping yourself. At least you only have to code it once.

It might be worth checking to see if there is a function that returns an
array for each row.

--
Ian Collins.
Jul 3 '06 #7
ey*******@gmail.com wrote:
Hi
let's say I have 20 variables named a1-a20 which are all of the same
type (int)
I need to access each one of them and according to the value do some
operation.
Is there some way I can access them inside a loop and build there name
dinamically or do I have to access each one of them seperatly?
Well, you could simply create an array and then parse the variable names
that come from your database and use the result as an array index.

Say your name is "a20", and your array is int a[20];

/* use name+1 to skip the leading 'a' */
idx = strtol (name+1, NULL, 10);

if (idx >= 1 && idx <= 20)
result = a[idx-1];

Jul 3 '06 #8
ey*******@gmail.com (in
11**********************@a14g2000cwb.googlegroups. com) said:

| Ian Collins wrote:
|| ey*******@gmail.com wrote:
||| Hi
||| let's say I have 20 variables named a1-a20 which are all of the
||| same type (int)
||| I need to access each one of them and according to the value do
||| some operation.
||| Is there some way I can access them inside a loop and build
||| there name dinamically or do I have to access each one of them
||| seperatly?
|||
|| No, but it sounds like you should store the values in an array.
|
| An array will be a good idea however I am getting these values from
| a DB table into a struct with a lot of other fields
| Is there a way to transform this struct? Or maybe some other way?

Names of variables aren't visible in the executable. Once in the
structure, you may be able to use a table of pointers to access each
of your 20 int structure members in a loop.

struct
{ int a1;
int b1;
char c[10];
int a2;
:
int a20;
} db_rec;

int *pointer_table[] = { &db_rec.a1, &db_rec.a2, /*...*/
,&db_rec.a20 };

for (i=1; i<21; i++)
{ value = *pointer_table[i];
/* do some value-dependent opperation */
}

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 3 '06 #9
Ian Collins wrote:

Please adjust your news reader so it does not quote signatures (the
bit under the --). This is normally a default setting.

Not for Google. The capability doesn't exist.


Brian
Jul 3 '06 #10
Default User wrote:
Ian Collins wrote:
Please adjust your news reader so it does not quote signatures (the
bit under the --). This is normally a default setting.


Not for Google. The capability doesn't exist.
But it's trivial enough that it can be manually done.

Jul 3 '06 #11
santosh wrote:
Default User wrote:
Ian Collins wrote:
Please adjust your news reader so it does not quote signatures
(the bit under the --). This is normally a default setting.

Not for Google. The capability doesn't exist.

But it's trivial enough that it can be manually done.
I would not disagree. It's important to remember that Google, while
it's fixed some things, still continues to handicap their users. Also
that many (not you of course) are newbies to newsgroups and can use
some help.

I changed my header display to include the user-agent line so I can
spot Google users:

User-Agent: G2/0.2

That way I can tailor my advice as needed.


Brian

Jul 3 '06 #12
Morris Dovey (in 0e**************@news.uswest.net) wrote:

some disgustingly incorrect C fragments (for statement corrected):

| struct
| { int a1;
| int b1;
| char c[10];
| int a2;
| :
| int a20;
| } db_rec;
|
| int *pointer_table[] = { &db_rec.a1, &db_rec.a2, /*...*/
| ,&db_rec.a20 };
|
for (i=0; i<20; i++)
| { value = *pointer_table[i];
| /* do some value-dependent opperation */
| }

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 4 '06 #13
ey*******@gmail.com wrote:
Ian Collins wrote:
ey*******@gmail.com wrote:
Ian Collins wrote:
>>ey*******@gmail.com wrote:
>>>Hi
>>>let's say I have 20 variables named a1-a20 which are all of the same
>>>type (int)
>>>I need to access each one of them and according to the value do some
>>>operation.
>>>Is there some way I can access them inside a loop and build there name
>>>dinamically or do I have to access each one of them seperatly?
>>
>>No, but it sounds like you should store the values in an array.
>>
>>--
>>Ian Collins.
>
Thanks
An array will be a good idea however I am getting these values from a
DB table into a struct with a lot of other fields
Is there a way to transform this struct? Or maybe some other way?
>
That depends on what the C wrapper to your database returns. Those I
have used return an array for each database row, so you should be able
to map to another array.

Is your code populating the struct?

--
Ian Collins.

The SQL that I am running on the DB to fetch the row, fetches it into
the struct. So the struct is being populated by the SQL.
The original suggestion is still correct. You should do the following:

type * arr[21];

arr[1] = &sqlres.a1;
arr[2] = &sqlres.a2;
...
arr[20] = &sqlres.a20;

Then (*arr[i]) will be a pointer alias to the ith variable. Keep in
mind that writing to these are unlikely to change your actual database
values, of course.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 5 '06 #14
On 4 Jul 2006 21:20:53 -0700, we******@gmail.com wrote:

snip
>The original suggestion is still correct. You should do the following:

type * arr[21];

arr[1] = &sqlres.a1;
arr[2] = &sqlres.a2;
...
arr[20] = &sqlres.a20;

Then (*arr[i]) will be a pointer alias to the ith variable. Keep in
mind that writing to these are unlikely to change your actual database
values, of course.
arr[i] is a pointer to the ith variable. *arr[i] is the ith variable
itself. I don't know what you intended the word alias to convey.
Remove del for email
Jul 6 '06 #15

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

Similar topics

6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
2
by: Bradford | last post by:
Question for the masses... Lets say I have variable with the following contents "aaaa bbbb ccccc dddd". The format is not specific and the space delimiter could be changed to any other. How...
4
by: Frederik Srensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
11
by: Ebenezer | last post by:
Let's suppose I have some nodes in an XML file, with an URL attribute: <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" /> <node...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.