473,405 Members | 2,160 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,405 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 1561
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave Smithz | last post by:
Having adopted someone else's PHP cope and completing a crash course in the language I came across a (probably common) problem with the current code. On a registration form, whenever users names...
5
by: Matthew Louden | last post by:
I want to know what's the best way to debug ASP pages? Now, I just put the following code when I got run-time error, but sounds like not efficient. Please advice! Thanks! Response.Write "Here"...
15
by: John J | last post by:
I've written the following code into a class to search for and display the results of all races entered (The complete code is in a previous thread). I wish to amend the code so as to display the...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
1
by: JohnH. | last post by:
Hi, Is there a better (best practice) way to do this? I have developed a large set of ASP.NET applications in C#. Like most applications I have a set of utility pages that can be called by...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
8
by: Paul Bromley | last post by:
Thanks for your tolerance on this list. I asked the question regarding Commercial Copy Protection along with Unique PC Idnetifier and obtaining the active IP address. This was all to identify and...
20
by: Joe | last post by:
Is any one charting packing considered to be the "best"? We've used ChartFX but wasn't too happy about the way data had to be populated along with some other issues which slip my mind right now and...
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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 project—planning, coding, testing,...
0
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...

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.