Hi folks,
New C# programmer here.
I am reading some CSV data from a file into an ArrayList. I want to get the
data from the ArrayList into a 2-dimensional array. I see a few references to
..Split, but I'm not sure that's what I need.
So, basically, what I have loaded into the ArrayList is:
"a, 1"
"b, 2"
"c, 3"
And what I want is a 2-d array with
array[0,0] == "a"
array[0,1] == "1"
array[1,0] == "b" etc.
Here's my code so far:
static void Main(string[] args)
{
//open streamreader to read in data file
StreamReader objReader = new StreamReader("c :\\data.csv");
string sLine="";
//read file in as an arraylist
ArrayList arrText = new ArrayList();
//read until end of file
while (sLine != null)
{
sLine = objReader.ReadL ine();
if (sLine != null)
//add each line of text file in
arrText.Add(sLi ne);
}
objReader.Close ();
//create the 2-d array
string [ , ] mydata;
//parse out the columns
//mystery code goes here
You guys are great. Thanks in advance for any help/advice.
Dave 1 21882
Here is one way I would do it. I use an ArrayList as I don't know before
hand how many lines I will parse:
private void button9_Click(o bject sender, System.EventArg s e)
{
// Split line on commas followed by zero or more spaces.
Regex splitRx = new Regex(@",\s*", RegexOptions.Co mpiled);
ArrayList al = new ArrayList();
using(StreamRea der sr = new StreamReader(@" c:\mycsv.csv"))
{
string line = null;
int ln = 0;
while((line = sr.ReadLine()) != null)
{
string[] fields = splitRx.Split(l ine);
if ( fields.Length != 2 )
{
Console.WriteLi ne("Invalid Input on line:"+ln);
continue;
}
ln++;
al.Add(fields);
}
}
Console.WriteLi ne("\nI processed {0} lines:", al.Count);
foreach(string[] sa in al)
{
Console.WriteLi ne("[{0}] [{1}]", sa[0], sa[1]);
}
}
------- mycsv.csv File ---------
a,1
b,2
c, 3
d, 4
--
William Stacey, MVP http://mvp.support.microsoft.com
"davehunt" <da******@discu ssions.microsof t.com> wrote in message
news:83******** *************** ***********@mic rosoft.com... Hi folks,
New C# programmer here.
I am reading some CSV data from a file into an ArrayList. I want to get
the data from the ArrayList into a 2-dimensional array. I see a few references
to .Split, but I'm not sure that's what I need.
So, basically, what I have loaded into the ArrayList is:
"a, 1" "b, 2" "c, 3"
And what I want is a 2-d array with
array[0,0] == "a" array[0,1] == "1" array[1,0] == "b" etc.
Here's my code so far:
static void Main(string[] args) { //open streamreader to read in data file StreamReader objReader = new StreamReader("c :\\data.csv"); string sLine="";
//read file in as an arraylist ArrayList arrText = new ArrayList();
//read until end of file while (sLine != null) { sLine = objReader.ReadL ine(); if (sLine != null) //add each line of text file in arrText.Add(sLi ne); } objReader.Close ();
//create the 2-d array string [ , ] mydata;
//parse out the columns //mystery code goes here
You guys are great. Thanks in advance for any help/advice.
Dave This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ben moretti |
last post by:
hi
i'm learning python, and one area i'd use it for is data management in
scientific computing. in the case i've tried i want to reformat a data
file from a normalised list to a matrix with some sorted columns. to
do this at the moment i am using perl, which is very easy to do, and i
want to see if python is as easy.
so, the data i am using is some epiphyte population abundance data for
particular sites, and it looks like this:
|
by: John Smith |
last post by:
I would like to parse a string into an array. I found on the net
the following codes which parse a string and print it. The result
is exactly what I want:
char * pch;
pch = strtok (buffer," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
|
by: chris |
last post by:
Hi there,
I created a Multidimensional array of labels
Label lblMultiArray = new Label { {Label3, LblThuTotal},
{Label4,LblFriTotal} };
Now I would like to compare the values in the array, comparing the text in
Label3 and LblThuTotal and the text in Label4 and LblFriTotal.
|
by: Kyle Teague |
last post by:
What would give better performance, serializing a multidimensional array
and storing it in a single entry in a table or storing each element of
the array in a separate table and associating the entries with the entry
in the other table?
Having a separate table would result in two queries instead of one, but
you wouldn't have to deal with the overhead of serializing and
unserializing data.
-- Kyle
|
by: Necromis |
last post by:
I am trying to figure out a way to change the below code to store data
in a multidemensional array rather than a listbox. The reason being is
I need to be able to pull each "currentrow" and manipulate the
position of each "currentfield" to then imput this data to another
source as a string. The delimited text might look like the following.
acctno, name, expdate,cr_line
4428123456789012,smith,0807,5000
4428123456789025,williams,0908,15000
| |
by: luftikus143 |
last post by:
Hi there,
I need to store three pieces of data - a result of a SQL query - in a multidimensional array, but don't really succeed. Sometimes it works, but then the output doesn't work accordingly.
My SQL result comes with something like this:
Africa | y2000 | 9430
Africa | y2005 | 9678
Europe | y2000 | 2314
where there are 6 regions and multiple years.
|
by: LittleCake |
last post by:
Hi All,
I have a multidimensional array where each sub-array contains just two
entries, which indicates a relationship between those two entries. for
example the first sub-array:
=Array
(
=30
=31
)
|
by: nine72 |
last post by:
Ok, I am at a complete loss on this and have finally come to the XML Parsing Gods (and perhaps a PHP minor deity) for guidance…
I will try my best to describe what I have going on…
1) I have 15 form pages, well over 500 potential fields, which are written in PHP. While most pages are one time entry forms, there are 5 that can be “recycled” as many times as needed. An example would be the Contacts Form. A user can give me 1 contact and move...
|
by: Slain |
last post by:
I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this
In the .h file
int *x;
in a initialize function:
x = new int;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |