2 dimensional array help
2 dimensional array help
2 dimensional array help
2 dimensional array help
2 dimensional array help
2 dimensional array help 2 dimensional array help 2 dimensional array help 2 dimensional array help 2 dimensional array help 2 dimensional array help 2 dimensional array help 2 dimensional array help
2 dimensional array help 2 dimensional array help
2 dimensional array help
Go Back  Xtreme Visual Basic Talk > > > 2 dimensional array help


Reply
 
Thread Tools Display Modes
  #1  
Old 12-02-2006, 01:06 AM
asdfqwerty asdfqwerty is offline
Newcomer
 
Join Date: Nov 2006
Posts: 7
Default 2 dimensional array help


Hello

I have an tab delineated m*n matrix stored as a .txt document which I am trying to read in and store as a two dimensional array.

i.e. txt file for a 2*5 matrix follows this format

1 [TAB] 2 [TAB] 3 [TAB] 4 [TAB] 5
6 [TAB] 7 [TAB] 8 [TAB] 9 [TAB] 10


Here is what I was thinking should work:

Code:
Dim WJ(intM, intN) As String Dim i, j, k As Integer Dim strLine As String Dim strTemp() As String strLine = SR.ReadLine strTemp = strLine.Split(Chr(9)) 'Reads a line a the txt document For i = 0 To (intM - 1) ' intM already declared For j = 0 To (intN - 1) 'intN already declared For k=0 To (intN-1) WJ(i, j) = (i , CInt(strTemp(k))) 'See NOTE


NOTE: IntelliSense highlights the comma directly after the i (on the right hand side of the equal sign on the last line) and is telling me a ')' is expected here...Any ideas?

Also, do I need the For loop for k=0..intN?


Thanks in advance!!

Last edited by asdfqwerty; 12-02-2006 at 04:33 PM.
Reply With Quote
  #2  
Old 12-02-2006, 04:03 AM
stevo2 dimensional array help stevo is offline
Senior Contributor

* Expert *
 
Join Date: May 2004
Location: Manchester, England.
Posts: 1,254
Default

Is the matrix always 2*5 or is there always 5 elements but more rows or both can be different in size ?
Reply With Quote
  #3  
Old 12-02-2006, 05:42 AM
jo0ls2 dimensional array help jo0ls is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2005
Location: London
Posts: 1,050
Default

Dim WJ(intM, intN) As String

So it's a 2D array that stores strings.

So, this makes sense:
WJ(0,0) = "hello world"

But this:
WJ(i, j) = (i , CInt(strTemp(k)))

doesn't work because (i , CInt(strTemp(k))) does not evaluate to a string. In fact it doesn't evaluate at all.
Reply With Quote
  #4  
Old 12-02-2006, 08:30 AM
asdfqwerty asdfqwerty is offline
Newcomer
 
Join Date: Nov 2006
Posts: 7
Default

Quote:
Originally Posted by stevo
Is the matrix always 2*5 or is there always 5 elements but more rows or both can be different in size ?
The matrix is declared as:

Dim WJ(,) As String then I ReDim WJ((intM-1),(intN-1)) where intM and intN are user inputs that correspond to the number of rows and number of columns respectively.

So to answer your question both intM and intN can be different in size.

Last edited by asdfqwerty; 12-02-2006 at 01:44 PM.
Reply With Quote
  #5  
Old 12-02-2006, 08:37 AM
asdfqwerty asdfqwerty is offline
Newcomer
 
Join Date: Nov 2006
Posts: 7
Default

Quote:
Originally Posted by jo0ls
Dim WJ(intM, intN) As String

So it's a 2D array that stores strings.

So, this makes sense:
WJ(0,0) = "hello world"

But this:
WJ(i, j) = (i , CInt(strTemp(k)))

doesn't work because (i , CInt(strTemp(k))) does not evaluate to a string. In fact it doesn't evaluate at all.
OK, thanks jo0ls, WJ(,) is now declared as Integer.


Now, in reference to the example below:

1 [TAB] 2 [TAB] 3 [TAB] 4 [TAB] 5
6 [TAB] 7 [TAB] 8 [TAB] 9 [TAB] 10

I want the array to hold these values:

WJ(0,0)=1 WJ(0,1)=2 WJ(0,2)=3 WJ(0,3)=4 WJ(0,4)=5
WJ(1,0)=6 WJ(1,1)=7 WJ(1,2)=8 WJ(1,3)=9 WJ(1,4)=10

Here is what my code now looks like (I realized the k was irrelevant):

Code:
Dim WJ(intM, intN) As String Dim i, j As Integer Dim strLine As String Dim strTemp() As String strLine = SR.ReadLine strTemp = strLine.Split(Chr(9)) 'Reads a line a the txt document For i = 0 To (intM - 1) ' intM already declared For j = 0 To (intN - 1) 'intN already declared WJ(i, j) = (i , CInt(strTemp(j)))

Last edited by asdfqwerty; 12-02-2006 at 04:33 PM.
Reply With Quote
  #6  
Old 12-02-2006, 10:07 AM
stevo2 dimensional array help stevo is offline
Senior Contributor

* Expert *
 
Join Date: May 2004
Location: Manchester, England.
Posts: 1,254
Default

Have a look at this, taking the data from the textfile and filling a 2d array
Code:
'check the file exists If IO.File.Exists("C:\yourfile.txt") Then 'read the lines into the array Dim Lines() As String = IO.File.ReadAllLines("C:\yourfile.txt") 'if the file has data If Lines.Length >= 1 Then 'split the first line so we can get the amount of elements Dim SplitLine() As String = Split(Lines(0), ControlChars.Tab) 'we can declare the 2d array now we know the bounds Dim WJ(Lines.GetUpperBound(0), SplitLine.GetUpperBound(0)) As Integer 'fill the array elements For i As Integer = 0 To WJ.GetUpperBound(0) SplitLine = Lines(i).Split(ControlChars.Tab) For k As Integer = 0 To WJ.GetUpperBound(1) WJ(i, k) = Integer.Parse(SplitLine(k)) Next Next End If End If
Reply With Quote
  #7  
Old 12-02-2006, 02:36 PM
asdfqwerty asdfqwerty is offline
Newcomer
 
Join Date: Nov 2006
Posts: 7
Default

Quote:
Originally Posted by stevo
Have a look at this, taking the data from the textfile and filling a 2d array
Code:
'check the file exists If IO.File.Exists("C:\yourfile.txt") Then 'read the lines into the array Dim Lines() As String = IO.File.ReadAllLines("C:\yourfile.txt") 'if the file has data If Lines.Length >= 1 Then 'split the first line so we can get the amount of elements Dim SplitLine() As String = Split(Lines(0), ControlChars.Tab) 'we can declare the 2d array now we know the bounds Dim WJ(Lines.GetUpperBound(0), SplitLine.GetUpperBound(0)) As Integer 'fill the array elements For i As Integer = 0 To WJ.GetUpperBound(0) SplitLine = Lines(i).Split(ControlChars.Tab) For k As Integer = 0 To WJ.GetUpperBound(1) WJ(i, k) = Integer.Parse(SplitLine(k)) Next Next End If End If

Ok I think I understand what you are saying, but why is Intellisense telling me 'ReadAllLines' is not a member of 'System.IO.File'
Reply With Quote
  #8  
Old 12-02-2006, 03:58 PM
stevo2 dimensional array help stevo is offline
Senior Contributor

* Expert *
 
Join Date: May 2004
Location: Manchester, England.
Posts: 1,254
Default

Right, ReadAllLines is part of framework 2.0, you must be using an earlier version. All you need to do is get the lines into the Lines() array so you could use the streamreader and readtoend
Code:
Dim SR As New IO.StreamReader("C:\yourfile.txt") Dim Lines() As String = Split(SR.ReadToEnd, ControlChars.NewLine) SR.Close()
Reply With Quote
  #9  
Old 12-02-2006, 05:53 PM
asdfqwerty asdfqwerty is offline
Newcomer
 
Join Date: Nov 2006
Posts: 7
Default

I am getting this error: Input string was not in a correct format.

It doesn't like the line WJ(i, j) = Integer.Parse(SplitLine(j)) towards the bottom.

I guess I do not totally understand what you were suggesting with the Integer.Parse...

Must I convert SplitLine() to an integer in some way?


Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim WJ(,) As String Dim SR As New IO.StreamReader("weightmatrix.txt") If IO.File.Exists("weightmatrix.txt") Then 'read the lines into the array Dim Lines() As String = Split(SR.ReadToEnd, ControlChars.NewLine) SR.Close() 'if the file has data If Lines.Length >= 1 Then 'split the first line so we can get the amount of elements Dim SplitLine() As String = Split(Lines(0), ControlChars.Tab) 'we can declare the 2d array now we know the bounds ReDim WJ(Lines.GetUpperBound(0), SplitLine.GetUpperBound(0)) Dim i, j As Integer 'fill the array elements For i = 0 To WJ.GetUpperBound(0) SplitLine = Lines(i).Split(ControlChars.Tab) For j = 0 To WJ.GetUpperBound(1) WJ(i, j) = Integer.Parse(SplitLine(j)) Next Next End If End If End Sub
Reply With Quote
  #10  
Old 12-02-2006, 06:08 PM
stevo2 dimensional array help stevo is offline
Senior Contributor

* Expert *
 
Join Date: May 2004
Location: Manchester, England.
Posts: 1,254
Default

Sorry, no need for the integer.parse, i thought you had declared wj as integer.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
2 dimensional array help
2 dimensional array help
2 dimensional array help 2 dimensional array help
2 dimensional array help
2 dimensional array help
2 dimensional array help 2 dimensional array help 2 dimensional array help 2 dimensional array help 2 dimensional array help 2 dimensional array help 2 dimensional array help
2 dimensional array help
2 dimensional array help
 
2 dimensional array help
2 dimensional array help
 
-->