471,337 Members | 952 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Best way to code the following

I need to read about 100 lines of a csv file into memory.

Each line contains 6 fields.

What is the best way to store this into memory and then read it back.

Is a array of a multi- dimsion array the way to do this.

If so I can read the data into an array using an arraylist for each line
and the fields in a string array but cannot see how I can get the data
back out.

Looking forward to comments
Sep 25 '07 #1
12 1500
Your question is problematic because it is open ended and subjective. What
does "Best Way" mean to you?

Personally, I would store the data in either a datatable or list<twhere t
= strongly typed class.

There are a bunch of samples on the web for reading and parsing CSV files.
These include an OLEDB solution, a regex solution, and plain 'ol procedural
parsing.
"Jeff" <jeff@[_nospam_].hardsoft.com.auwrote in message
news:13************@corp.supernews.com...
>I need to read about 100 lines of a csv file into memory.

Each line contains 6 fields.

What is the best way to store this into memory and then read it back.

Is a array of a multi- dimsion array the way to do this.

If so I can read the data into an array using an arraylist for each line
and the fields in a string array but cannot see how I can get the data
back out.

Looking forward to comments

Sep 25 '07 #2

Properly reading a CSV file can be a lot more complicated than simply
splitting strings. Here's an excellent CSV reader.

A Fast CSV Reader
http://www.codeproject.com/cs/database/CsvReader.asp

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Tue, 25 Sep 2007 23:40:17 +1000, Jeff
<jeff@[_nospam_].hardsoft.com.auwrote:
>I need to read about 100 lines of a csv file into memory.

Each line contains 6 fields.

What is the best way to store this into memory and then read it back.

Is a array of a multi- dimsion array the way to do this.

If so I can read the data into an array using an arraylist for each line
and the fields in a string array but cannot see how I can get the data
back out.

Looking forward to comments
Sep 25 '07 #3
Its not the reading or writing the CSV file which is the issue it is I
cannot find an example of how to place data into a multi-demsendion
array and then how to get the data back out of the arral.

As far as I can see I must use an ArrayList so it can grow dynamically.

Regards
Jeff

Jeff wrote:
I need to read about 100 lines of a csv file into memory.

Each line contains 6 fields.

What is the best way to store this into memory and then read it back.

Is a array of a multi- dimsion array the way to do this.

If so I can read the data into an array using an arraylist for each line
and the fields in a string array but cannot see how I can get the data
back out.

Looking forward to comments
Sep 25 '07 #4
Jeff wrote:
Its not the reading or writing the CSV file which is the issue it is I
cannot find an example of how to place data into a multi-demsendion
array and then how to get the data back out of the arral.

As far as I can see I must use an ArrayList so it can grow dynamically.
I think your question is still not very clear.

However, assuming ArrayList would be one possible implementation, you
should consider using the generic List<class instead. It provides
much of the same functionality, but as a safely typed collection object.

Of course, this assumes that the data you're putting into the list has a
consistent type. If not, then perhaps ArrayList is the right solution
after all.

Pete
Sep 25 '07 #5
Ok I give an example

My lines consists of

String1, string2, string3, string4, string5

So this would be a string[5] as it will always be 5 items in a line.

Now i add this to an arraylist

string[] myFields = new string[5];
ArrayList myFile = new ArrayList()
myFile.Add(myFields);

I can do this and build my array

The problem is how do i read the myFields from the myFile array
Sep 25 '07 #6
As amdrit wrote there are plenty of solutions, if you want to access a
multidimensional array, then in my idea just take a datatable, in
combination with the OleDB connection it is the most easiest thing to use.

www.connectionstrings.com

Cor
"Jeff" <jeff@[_nospam_].hardsoft.com.auschreef in bericht
news:13*************@corp.supernews.com...
Ok I give an example

My lines consists of

String1, string2, string3, string4, string5

So this would be a string[5] as it will always be 5 items in a line.

Now i add this to an arraylist

string[] myFields = new string[5];
ArrayList myFile = new ArrayList()
myFile.Add(myFields);

I can do this and build my array

The problem is how do i read the myFields from the myFile array
Sep 25 '07 #7
Jeff wrote:
[...]
string[] myFields = new string[5];
ArrayList myFile = new ArrayList()
myFile.Add(myFields);

I can do this and build my array

The problem is how do i read the myFields from the myFile array
What part of that problem exactly are you having trouble with?

Do you understand the use of ArrayList generally? That is, how you
would use it with any other object type? If so, it's the same, where in
this case your object is a string array type (ie "string[]").

If you don't understand how to use ArrayList, you should say so. That
way, we can tailor the answers to more usefully describe how to use
ArrayList.

Pete
Sep 25 '07 #8

Perhaps the problem is your assumption that you want to put the data
into a multidimensional array in the first place. Why do you want to
do this? What do you really want to do with the data? Putting into
the array implies multi-pass handling of data (one pass to read it
from csv into array, and a second pass through the array to do
whatever you really want to do with the data). If you can just get
directly from CSV to result then there is no need for the array.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.


On Wed, 26 Sep 2007 05:24:40 +1000, Jeff
<jeff@[_nospam_].hardsoft.com.auwrote:
>Its not the reading or writing the CSV file which is the issue it is I
cannot find an example of how to place data into a multi-demsendion
array and then how to get the data back out of the arral.

As far as I can see I must use an ArrayList so it can grow dynamically.

Regards
Jeff
Sep 25 '07 #9
On Tue, 25 Sep 2007 23:40:17 +1000, Jeff
<jeff@[_nospam_].hardsoft.com.auwrote:
>I need to read about 100 lines of a csv file into memory.

Each line contains 6 fields.

What is the best way to store this into memory and then read it back.

Is a array of a multi- dimsion array the way to do this.

If so I can read the data into an array using an arraylist for each line
and the fields in a string array but cannot see how I can get the data
back out.

Looking forward to comments
You haven't given enough details. However, I think an array would be a
bad idea - surely each column in the csv could have a different
datatype? I would personally use a List<Tfor this. You can use the
set property of your List<Tobject to naturally validate data entry to
the list. In general, I prefer List<Tto ArrayList because List<Tis
easier to use - none of those irritating casts to bother with.
Sep 26 '07 #10
Jeff wrote:
Ok I give an example

My lines consists of

String1, string2, string3, string4, string5

So this would be a string[5] as it will always be 5 items in a line.

Now i add this to an arraylist

string[] myFields = new string[5];
ArrayList myFile = new ArrayList()
myFile.Add(myFields);

I can do this and build my array

The problem is how do i read the myFields from the myFile array
foreach (string[] myStringArray in myFile)
{
Console.WriteLine(myStringArray[0]);
etc.
}

OR

for (int i = 0; i < myFile.Count; i++)
{
string[] myStringArray = (string[])myFile[i];
Console.WriteLine(myStringArray[0]);
etc.
}

Is this what you are asking about - the syntax of accessing an ArrayList?

HTH,
-rick-
Sep 26 '07 #11
On Sep 25, 12:24 pm, Jeff <jeff@[_nospam_].hardsoft.com.auwrote:
Its not the reading or writing the CSV file which is the issue it is I
cannot find an example of how to place data into a multi-demsendion
array and then how to get the data back out of the arral.

As far as I can see I must use an ArrayList so it can grow dynamically.

Regards
Jeff

Jeff wrote:
I need to read about 100 lines of a csv file into memory.
Each line contains 6 fields.
What is the best way to store this into memory and then read it back.
Is a array of a multi- dimsion array the way to do this.
If so I can read the data into an array using an arraylist for each line
and the fields in a string array but cannot see how I can get the data
back out.
Looking forward to comments- Hide quoted text -

- Show quoted text -

Jeff, others have given you great advice.

Here are some specifics that might help.

First, create a class to store each record from your csv. Something
like this:

public class Record
{
public Record(string field1, string field2, string field3) {
_field1 = field1;
_field2 = field2;
_field3 = field3;
}
private string _field1;
private string _field2;
private string _field3;

public void DoSomething() {
Console.WriteLine("{0}, {1} and {2}",
_field1, _field2, _field3);
}
}
As stated in the replies to your original message, the recommended
(and .NET 2.0+) approach is to use Generics (List<Tspecifically).

List<Record_myRecordsGen = new List<Record>();
// Create a list of Record objects. (You mentioned you had no
problem extracting
// data from your data source).
_myRecordsGen.Add(new Record("f1 of 1 (gen)", "f2 of 1 (gen)", "f3
of 1 (gen)"));
_myRecordsGen.Add(new Record("f1 of 2 (gen)", "f2 of 2 (gen)", "f3
of 2 (gen)"));
_myRecordsGen.Add(new Record("f1 of 3 (gen)", "f2 of 3 (gen)", "f3
of 3 (gen)"));

// Get your Record objects out of the list (and do something with
each).
foreach (Record record in _myRecordsGen) {
record.DoSomething();
}
You mentioned you think you might need to use an ArrayList. This is
true if you're using .NET 1.x. Here's the same example using an
ArrayList.

ArrayList _myRecordsAL = new ArrayList();
// Create a list of Record objects.
_myRecordsAL.Add(new Record("f1 of 1 (al)", "f2 of 1 (al)", "f3 of
1 (al)"));
_myRecordsAL.Add(new Record("f1 of 2 (al)", "f2 of 2 (al)", "f3 of
2 (al)"));
_myRecordsAL.Add(new Record("f1 of 3 (al)", "f2 of 3 (al)", "f3 of
3 (al)"));

// Get your Record objects out of the list (and do something with
each).
foreach (Record record in _myRecordsAL) {
record.DoSomething();
}


HTH
-Jay

Sep 26 '07 #12
Thanks all I have learn't from this.
Sep 29 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Matthew Louden | last post: by
15 posts views Thread by John J | last post: by
136 posts views Thread by Matt Kruse | last post: by
17 posts views Thread by 2005 | last post: by
20 posts views Thread by Joe | last post: by

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.