multidimensional arrays

reading in statistics and putting it into a multidimensional array

Page 1 of 1

3 Replies - 2005 Views - Last Post: 22 July 2008 - 02:16 PM Rate Topic: -----

#1 sss_boi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-July 08

multidimensional arrays

Post icon  Posted 21 July 2008 - 11:21 PM

hi

i am wanting to read in some statistics from an excel file and then put them into a multi dimensional array, but im unsure how to do this, ill show you what i have so far.

 Public Class frmNumbat
	Dim sReader As IO.StreamReader
	Dim numarr(,,,) As Double
	Dim numdata As String
	Dim comma1 As Integer
	Dim a, b, c, d As Double
	Private Sub txtInputFile_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInputFile.TextChanged

		sReader = IO.File.OpenText("C:\Users\Ben Cousins\Downloads\datanumbats.csv")
		numdata = sReader.ReadLine()
		Do While (sReader.EndOfStream)
			comma1 = numdata.IndexOf(",")
			numarr(a, comma1, comma1 + 1, comma1 + 1 + 1) = CDbl(numdata.Substring(0, comma1)) 'comma1 lots of characters

			MessageBox.Show("numarr")
		Loop
	End Sub



as all uni students do, i have left it to the last minute and kinda havent been upto date with all the work, so i may take a little while to learn what you are saying.

i couldnt attach the statistics so i uploaded it onto a fileshare website
http://www.2shared.c...ataNumbats.html

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: multidimensional arrays

#2 kikz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 32
  • Joined: 21-July 08

Re: multidimensional arrays

Posted 22 July 2008 - 01:45 AM

Have you heard of the Split() method? It takes a string and returns a string element, breaking the elements (tokenizing) on the particular tokenizer you supply. That could be a comma...

That way you could split on a line by line basis from the CSV file. Then you could iterate through the results and add them into the multi-dimensional array. Alternatively, have you learnt about Jagged arrays yet? That would be a far easier way to go.

Remember, too, the CSV file specification says fields can also contain comma's. 1, "My Name", 345, "My Street, My Suburb" for an example. Where "My Street, My Suburb" is a valid single field. Your method (and the split method) won't accomodate for that.

Are you allowed to use 3rd party CSV libraries? There are a few CSV optimised subclasses of DataReader out there (for free).
You could write some code yourself, using an OleDbConnection, with connection string "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & directory & ";Extended Properties=""Text;HDR=Yes;FMT=Delimited""". Then you can access the CSV data as if it were a table :). That might be over your head though ;)

I'll leave you to have another crack at it (I'm not sure what constitutes giving too much help on assignments).

This post has been edited by kikz: 22 July 2008 - 01:47 AM

Was This Post Helpful? 0
  • +
  • -

#3 sss_boi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-July 08

Re: multidimensional arrays

Posted 22 July 2008 - 03:50 AM

i may have learnt the split method but im not sure, what i really need help is to be able to write a loop that will loop through the csv file and add a counter

something along the lines of

 Do While (Not sReader.EndOfStream)
				numData = sReader.ReadLine()
				count = count + 1
			Loop
			sReader.Close()




is that correct?

This post has been edited by sss_boi: 22 July 2008 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 kikz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 32
  • Joined: 21-July 08

Re: multidimensional arrays

Posted 22 July 2008 - 02:16 PM

It depends on what you're wanting to count. If you want to count the number of records (lines) in your CSV file, then what you have is correct.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1