Link to home
Start Free TrialLog in
Avatar of Pyromanci
PyromanciFlag for United States of America

asked on

Create a associative Array in C

Hello

This is my first time ever needing any sort of associative array in C. Reason why i am looking for this is because I am trying to Addapt a flat file program to use MySQL. I just figured for anyone else making changes to it, it would be easier to do so with a associative array then having to keep track of column numbers. especially since some of these tables are going to be over 100 columns in length.

SOLUTION
Avatar of masheik
masheik
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, and masheik : everybody can do a quick Google search and paste the first few results here. But that generally isn't appreciated on this site. It doesn't show that you know what you're talking about, so it doesn't give the asker any confidence that what you say is valid. It also doesn't have a lot of value for the PAQ database (links break).

Please, if you know the answer to a question, take your time to properly write down your response. It will be better for everybody involved.
Thanks for the advice infinity08,

i thought it will be somehow help the asker to find a solution.I will take care to answer questions in future
Great, that's all one can ask :)
I do not get the point of your mail Infinity08. The question is:
"because I am trying to Addapt a flat file program to use MySQL"

Ok if I got that right he has a file but likes to put it into database, You'r code queries the database. So either I'm wrong or  you are. For me it seems what  he likes is a way to import flat files into Mysql, and for tha there are extra "READ DATA INFILE" for that.

I can not see how some assiciative array could be of any help for that.

with best regards
Friedrich

>> So either I'm wrong or  you are.

My interpretation was that the current version of the application uses a flat file for storing the data. And the next evolution of the application would store the data in a MySQL database.

So, the code that deals with reading/writing from/to a flat file has to be replaced with code reading/writing from/to a MySQL database.

I could be wrong though ... So, let's see what Pyromanci says :)


Re. the associative array bit. The question stated this :

>> it would be easier to do so with a associative array then having to keep track of column numbers.

In other words, rather than referring to a column by its index, he'd like to refer to a column using the name of the column.
An associative array is one way of doing that (mapping column names to column indexes), but the approach I suggested, using enums, seems easier and, and thus preferable.
Avatar of Pyromanci

ASKER

Well thanks for all the replies.

OK let me go into further detail.The reason i am going from a flat file storage to a MySQL database store is for a web integration and because the program is already Open Source (and i am just making a sub version of the program) I figure it would be easier for people to have to deal with associative arrays over having to have to deal with column numbers.

Ok lets go into a example.

OK, I already know i am going to have a table name sysconfig with 2 columns (Lets say coloumn A and Column B), and lets say in the code this table is called to for read and write 50 times (also this program is currently roughly 500K lines in multiple source files). Now lets say someone in the future needs to alter the table to add a third column (column C) in to meet their needs and places the column after column A. And they only need that third column in 1 of those functions.

So without dealing with a associative array (ie. row["A"], row["B"], row["C"]) the person would then have to find all the references to that table and modify all calls for row[1] to row[2] just to make the change for 1 place.

Ideal i would have a function built into the core code to create this associative array. So that way it can just generate them dynamically based on the results. I know if it were possible i would have to call several mysql api functions and have a few loops.
As I thought :) You don't really need an associative array. A simple enum would be more than sufficient (and definitely easier). See my first post for an example.
Infinity, problem is with the enum i would have to go through a and define every single column for every single table. right now i am working on the database layout. I am currently at 53 tables each table ranging from 2 columns up to 124 columns. So in my mind doing the enum method is not the best way.
>> with the enum i would have to go through a and define every single column for every single table

How would that be different from what you have to do with an associative array ?
For my taste the stuff does not fit very well with. databases. a table does not have two columns sometime, and hundred columns another time. A table is "fixed" insofar as it's really some work  to add columns. So Infinity is IMHO partly right. If you do not like enums just juse an array an index into it. But first you better made up your mind on what your table might look like. And even if he'd now use a flat file he probalby could register it as an ODBC Data source and soe you could  run queris over it. But for me it seem he does not even know when there are how many columns and  how they are named. Howerver one could imageing somethinglike this

struct table_entry {
    size_t line_index;
     char ** rows;
     size_t number_of_rows;
};

so he could read in  the flat file, line by line, he then splits the  line into tokens and keep record on how many are in there. So he easily can access earch record (assuming that each line stands for one "table row"), this could be placed in an array,

But I can not see how you can put such different lines into some database. That's simply not how relational databases work. So maybe he'd better be off with some "persistence layer" for C as e.g BerkeleyDB. Or something else like e.g YAML Format or something which is better suitable for that kind of unstructured data.

But for  a relational  database he has to come up with some sound Database structure....
Ok, the flat files will be gone for good once i complete these changes. I am hand converting the base data into the mysql database as i go through this conversion.

after reading everyone's post i am going to just have to combine some of these ideas. I believe this function should handle my needs.

#include <ctia.h>
 
Array mysql_fetch_assoc (MYSQL_RES result)
{
	unsigned int num_fields;
	unsigned int i;
	MYSQL_FIELD *fields;
	MYSQL_ROW row;
	Array <std::string> assc_row;
	
	row = mysql_fetch_row(result);
	num_fields = mysql_num_fields(result);
	fields = mysql_fetch_fields(result);
	for(i = 0; i < num_fields; i++)
	{
		assc_row[fields[i].name] = row[i];
	}
 
	return assc_row;
}

Open in new window

The above code will not work as expected. The assignment
assc_row[fields[i].name] = row[i];

Won't work, if I'm not mistaken.

Regards
Friedrich