473,396 Members | 2,030 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,396 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 1274
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.