473,585 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1577
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.a uwrote in message
news:13******** ****@corp.super news.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.a uwrote:
>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(myFi elds);

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
multidimensiona l 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.a uschreef in bericht
news:13******** *****@corp.supe rnews.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(myFi elds);

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(myFi elds);

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 multidimensiona l 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.a uwrote:
>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.a uwrote:
>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

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

Similar topics

11
2283
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 have an apostrophe in them it causes problems as they do not get added to the DB correctly for reasons that immediately become apparent. Before...
5
28464
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" Response.End
15
2219
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 best result only. Can anyone suggest a simple amendment to the following that will result in only the best result being displayed?
136
9282
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to...
1
1216
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 applications or directly by users. On my own I have developed a technique to manage the application and lib directories, but it seems a little...
16
2788
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 efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. ...
17
3021
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 else? The reason for this question is that I had an assignment in which I was
8
11366
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 tie down software to each PC on a network. I have just realised that the unique identity that I need to go for on the network and should be easy to...
20
10015
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 Dundas has bugs and doesn't do a good enough job displaying axis labels and is very slow to paint large numbers of series and data points. We're...
13
2303
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 add-ons for this purpose? If not, any urls which demonstrates code snippets management?
0
8195
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8334
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7947
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6596
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5707
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5386
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3831
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3856
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1444
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.